试图让VBA从Zillow提取数据

时间:2018-06-21 21:08:23

标签: excel vba excel-vba

我是编码的新手,一直在尝试找出如何从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。

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)

您想要的值是具有className value的第一个元素。您可以使用querySelector来应用.value的CSS选择器(其中"."是类的选择器)来获取该值。

Option Explicit
Public Sub GetInfo()
    Dim html As New MSHTML.HTMLDocument
    Const URL As String = "https://www.zillow.com/new-york-ny/home-values/"
    html.body.innerHTML = GetHTML(URL)
    Debug.Print html.querySelector(".value").innerText
End Sub

Public Function GetHTML(ByVal URL As String) As String
    Dim sResponse As String
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", URL, False
        .send
        sResponse = StrConv(.responseBody, vbUnicode)
    End With
    GetHTML = Mid$(sResponse, InStr(1, sResponse, "<!DOCTYPE "))
End Function

您还可以使用:

Debug.Print html.getElementsByClassName("value")(0).innerText

当前网页价值:

webpage


代码输出:

code output