VBA:Web抓取并在Excel电子表格中显示结果

时间:2019-01-30 08:38:41

标签: excel vba web-scraping

我有一个VBA代码,可以在Zillow上成功搜索房屋,但是我试图提取网站上显示状态的部分(class =“ status”),并在excel电子表格上显示结果。

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

最终,我还希望VBA从电子表格中提取数据并通过按一下按钮自动加载每个数据。

例如:

111 Main st [为Zillow加载111的按钮]

211 Main st [为211加载Zillow的按钮]

311 Main st [为311加载Zillow的按钮] ...

但是,如果至少能在excel上显示class =“ status”部分,我将不胜感激

1 个答案:

答案 0 :(得分:2)

您可以使用CSS类选择器

HTMDoc.querySelector(".status").innerText

有关循环URL的示例,请参见here