shahin
Active Member
Hi there everyone! I've already completed building a scraper which is parsing a web data very smoothly. However, in my code I've used "Htmlhtmlelement" twice within a for loop to get the work done but it doesn't look good, I meant not well organized. Is it possible to use that aforementioned object once in a for loop to make the code look clean. I'm pasting here the code for your consideration. Thanks in advance.
Code:
Sub ParseHackerNews()
Dim http As New ServerXMLHTTP60, html As New HTMLDocument
Dim topics As Object, posts As Object, post As HTMLHtmlElement, data As HTMLHtmlElement
With http
.Open "GET", "https://news.ycombinator.com/", False
.send
html.body.innerHTML = .responseText
End With
Set topics = html.getElementsByClassName("athing")
Set posts = html.getElementsByClassName("subtext")
On Error Resume Next
For x = 0 To topics.Length - 1
Set data = topics(x)
i = i + 1
Cells(i, 1).Value = data.getElementsByClassName("storylink")(0).innerText
Cells(i, 2).Value = data.getElementsByClassName("sitestr")(0).innerText
Set post = posts(x)
Cells(i, 3).Value = post.getElementsByClassName("score")(0).innerText
Cells(i, 4).Value = post.getElementsByClassName("hnuser")(0).innerText
Next x
Set http = Nothing: html = Nothing: topics = Nothing: posts = Nothing
End Sub