使用 VBA 从 Excel 消息框将数据提取到电子表格中(从 Zillow 提取的数据)
Pulling data from Excel Message-box into spreadsheet using VBA (Data pulled from Zillow)
我是编码新手,一直在尝试弄清楚如何从 zillow 中提取特定数据并将其导入 excel。老实说,我很迷茫,我一直在寻找整个表格和其他在线视频,但我没有任何运气。
这是我正在使用的网站 link https://www.zillow.com/new-york-ny/home-values/
我希望将所有数字拉入 excel 以便我可以 运行 进行一些计算。如果有人能帮助我将 660,000 美元的 Zillow 房屋价值指数拉入 excel,我觉得我可以算出其余部分。
这是网站上的代码
<ul class="value-info-list" id="yui_3_18_1_1_1529698944920_2626">
<li id="yui_3_18_1_1_1529698944920_2625">
<!-- TODO: need zillow logo icon here -->
<!-- <span class="zss-logo-color"><span class="zss-font-icon"></span></span> -->
<span class="value" id="yui_3_18_1_1_1529698944920_2624">
0,000
</span>
<span class="info zsg-fineprint"> ZHVI
</span>
我尝试了 getElementsByTagName getElementById 和 getElemenByClass ID 让我感到困惑,因为我希望能够将任何城镇输入 excel,它会在 zillow 上搜索网页上的数据.所有的 id 标签都是不同的,所以如果我在这段代码中按 id 搜索,它将不适用于其他城镇。我使用了 Class 标签并获得了一些我正在寻找的数据。
这是我想出的代码它将 660,000 美元拉入消息框。 Range 函数正在运行并将消息框数据放入 excel。这是拉出一堆字符串,我能够拉出 660,000 美元,但是设置的方式我不确定如何拉出剩余的数据,例如 1 年预测 "yr_forcast" 是单元格范围我想将数据拉入 excel。我还希望将剩余的数字从这个消息框中提取出来以进行估值工作。
我还附上了消息框的屏幕截图,这样您就可以看到我在看什么。
Link to screenshot of message box
Sub SearchBot1()
'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
Dim Doc As HTMLDocument 'holds document object for internet explorer
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://www.zillow.com/new-york-ny/home-values/"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'in the search box put cell "A2" value, the word "in" and cell "C1" value
objIE.document.getElementById("local-search").Value = _
Sheets("Sheet2").Range("B3").Value & ", " & Sheets("Sheet2").Range("B4").Value
'click the 'go' button
Set the_input_elements = objIE.document.getElementsByTagName("button")
For Each input_element In the_input_elements
If input_element.getAttribute("name") = "SubmitButton" Then
input_element.Click
Exit For
End If
Next input_element
'wait again for the browser
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'price for home
Set Doc = objIE.document
Dim cclass As String
cclass = Trim(Doc.getElementsByClassName("value-info-list")(0).innerText)
MsgBox cclass
Dim aclass As Variant
aclass = Split(cclass, " ")
Range("Market_Price").Value = aclass(0)
Range("yr_forecast").Value = aclass(5)
'close the browser
objIE.Quit
End Sub
如果您需要更多信息,请告诉我。
您可以使用 Children
集合从元素的后代获取数据。第一个子节点的索引为 0:
cclass = Trim(Doc.getElementsByClassName("value-info-list")(0).Children(0).Children(0).innerText)
我是编码新手,一直在尝试弄清楚如何从 zillow 中提取特定数据并将其导入 excel。老实说,我很迷茫,我一直在寻找整个表格和其他在线视频,但我没有任何运气。
这是我正在使用的网站 link https://www.zillow.com/new-york-ny/home-values/
我希望将所有数字拉入 excel 以便我可以 运行 进行一些计算。如果有人能帮助我将 660,000 美元的 Zillow 房屋价值指数拉入 excel,我觉得我可以算出其余部分。
这是网站上的代码
<ul class="value-info-list" id="yui_3_18_1_1_1529698944920_2626">
<li id="yui_3_18_1_1_1529698944920_2625">
<!-- TODO: need zillow logo icon here -->
<!-- <span class="zss-logo-color"><span class="zss-font-icon"></span></span> -->
<span class="value" id="yui_3_18_1_1_1529698944920_2624">
0,000
</span>
<span class="info zsg-fineprint"> ZHVI
</span>
我尝试了 getElementsByTagName getElementById 和 getElemenByClass ID 让我感到困惑,因为我希望能够将任何城镇输入 excel,它会在 zillow 上搜索网页上的数据.所有的 id 标签都是不同的,所以如果我在这段代码中按 id 搜索,它将不适用于其他城镇。我使用了 Class 标签并获得了一些我正在寻找的数据。
这是我想出的代码它将 660,000 美元拉入消息框。 Range 函数正在运行并将消息框数据放入 excel。这是拉出一堆字符串,我能够拉出 660,000 美元,但是设置的方式我不确定如何拉出剩余的数据,例如 1 年预测 "yr_forcast" 是单元格范围我想将数据拉入 excel。我还希望将剩余的数字从这个消息框中提取出来以进行估值工作。
我还附上了消息框的屏幕截图,这样您就可以看到我在看什么。 Link to screenshot of message box
Sub SearchBot1()
'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
Dim Doc As HTMLDocument 'holds document object for internet explorer
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://www.zillow.com/new-york-ny/home-values/"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'in the search box put cell "A2" value, the word "in" and cell "C1" value
objIE.document.getElementById("local-search").Value = _
Sheets("Sheet2").Range("B3").Value & ", " & Sheets("Sheet2").Range("B4").Value
'click the 'go' button
Set the_input_elements = objIE.document.getElementsByTagName("button")
For Each input_element In the_input_elements
If input_element.getAttribute("name") = "SubmitButton" Then
input_element.Click
Exit For
End If
Next input_element
'wait again for the browser
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'price for home
Set Doc = objIE.document
Dim cclass As String
cclass = Trim(Doc.getElementsByClassName("value-info-list")(0).innerText)
MsgBox cclass
Dim aclass As Variant
aclass = Split(cclass, " ")
Range("Market_Price").Value = aclass(0)
Range("yr_forecast").Value = aclass(5)
'close the browser
objIE.Quit
End Sub
如果您需要更多信息,请告诉我。
您可以使用 Children
集合从元素的后代获取数据。第一个子节点的索引为 0:
cclass = Trim(Doc.getElementsByClassName("value-info-list")(0).Children(0).Children(0).innerText)