Home » excel » VBA: Web scraping and displaying results in Excel spreadsheet

VBA: Web scraping and displaying results in Excel spreadsheet

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a VBA code that successfully searches a house on Zillow, however I’m trying to extract the part of the website that shows the status (class=”status”) and display the results on an excel spreadsheet.

Sub getHTMLdocument()

Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLButton As MSHTML.IHTMLElement
Dim stat As MSHTML.IHTMLElement

IE.Visible = True
IE.navigate "zillow.com/homes"

Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop

Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("citystatezip")
HTMLInput.Value = "21371 Estepa Circle Huntington Beach"

Set HTMLButtons = HTMLDoc.getElementsByTagName("button")
HTMLButtons(0).Click

Set stat = HTMLDoc.getElementById("yui_3_18_1_1_1548832597156_4287")
    Debug.Print stat.getAttribute("href")

End Sub

Eventually I also want the VBA to pull data from a spreadsheet and load each automatically at the push of a button.

Ex:

111 Main st [Button that loads Zillow for 111]

211 Main st [Button that loads Zillow for 211]

311 Main st [Button that loads Zillow for 311]…

However, I’d really appreciate it if I could at least get it to display the class=”status” part on excel

How to&Answers:

You can use a css class selector

HTMDoc.querySelector(".status").innerText

See here for an example of looping urls.