Get Information From XML From A URL

2020-05-02 18:34发布

I'm trying to extract information from this URL https://www.zillow.com/webservice/GetSearchResults.htm?zws-id=X1-ZWz18hxnbvw0ej_40oka&address=2912%20WEST%201ST%20STREET%20UNIT%201&citystatezip=Jacksonville%20FL%2032254

The information I want to extract is 102557, it can be found from this portion of the XML URL:

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

I'm using this code to extract it and its giving me an "Object variable or With block variable not set" error message.

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


标签: excel xml vba
1条回答
够拽才男人
2楼-- · 2020-05-02 19:18

Try this, it worked for me. You'll just need to modify it slightly to work for you ...

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

... obviously I stripped it back to the raw call so I could target the specifics.

This also works ...

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

... just be careful with that one though.

查看更多
登录 后发表回答