使用VBA将数据从Excel消息框中提取到电子表格中(从Zillow中提取数据)

时间:2018-06-25 21:46:35

标签: excel vba excel-vba zillow

我是编码的新手,一直在尝试找出如何从zillow中提取特定数据并将其导入到excel中。老实说,我很想弄清楚这一点,而且我一直在浏览表格和其他在线视频,但是我没有任何运气。

这是指向我正在使用的网站的链接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">
        $660,000
    </span>

    <span class="info zsg-fineprint"> ZHVI

    </span>

我尝试了getElementsByTagName getElementById和getElemenByClass该ID使我感到困惑,因为我希望能够将任何城镇输入excel,它将在zillow上搜索网页上的数据。所有的id标记都是不同的,因此,如果我在此代码中按id搜索,则不适用于其他城镇。我使用了Class标记,并且能够获取我正在寻找的一些数据。

这是我想出的代码,它将$ 660,000放入消息框。范围功能正在起作用,并将消息框数据放入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

如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

您可以使用Children集合从element的后代获取数据。第一个子节点的索引为0:

cclass = Trim(Doc.getElementsByClassName("value-info-list")(0).Children(0).Children(0).innerText)
相关问题