通过URL从XML获取信息

时间:2019-04-15 09:31:04

标签: excel xml vba

我正在尝试从此URL https://www.zillow.com/webservice/GetSearchResults.htm?zws-id=X1-ZWz18hxnbvw0ej_40oka&address=2912%20WEST%201ST%20STREET%20UNIT%201&citystatezip=Jacksonville%20FL%2032254提取信息

我要提取的信息是102557,可以从XML URL的这一部分找到:

<zestimate>
<amount currency="USD">102557</amount>

我正在使用这段代码来提取它,并给我一个“对象变量或未设置块变量”的错误消息。

Sub GetInfo2()

    Dim Http As New XMLHTTP60, Html As New HTMLDocument
    Dim lastrow As Long, i As Long
    Dim sdd As String
    Dim add As Variant
    Dim url As Range

    i = 2

    For Each url In Range(Cells(3, "M"), Cells(Rows.Count, "M").End(xlUp))
        With Http
            .Open "GET", url, False
            .send
            Html.body.innerHTML = .responseText
        End With


        sdd = Html.querySelector("span[class='zsg-tooltip-launch_keyword']")(0).innerText
        i = i + 1
        DD.Cells(i, "J") = sdd

    Next url


End Sub


1 个答案:

答案 0 :(得分:1)

尝试一下,它对我有用。您只需要稍作修改即可为您工作...

Sub GetInfo2()
    Dim objHttp As XMLHTTP60, objXml As DOMDocument60, strUrl As String

    Set objHttp = New XMLHTTP60
    Set objXml = New DOMDocument60

    strUrl = "https://www.zillow.com/webservice/GetSearchResults.htm?zws-id=X1-ZWz18hxnbvw0ej_40oka&address=2912%20WEST%201ST%20STREET%20UNIT%201&citystatezip=Jacksonville%20FL%2032254"

    With objHttp
        .Open "GET", strUrl, False
        .send
    End With

    If objXml.LoadXML(objHttp.responseText) Then
        Debug.Print objXml.DocumentElement.SelectSingleNode("response/results/result/zestimate/amount").Text
    End If
End Sub

...显然,我将其剥离回了原始调用,以便可以确定具体目标。

这也可以...

objXml.DocumentElement.SelectSingleNode("//amount").Text

...不过要小心一点。