首页 > 解决方案 > Unable to grab a portion of text from a website using post http requests

问题描述

I'm trying to parse the text within yellow colored area from a website which is visible when you fill in the inputbox next to parcel id and hit the search button. Here is an example parcel id 01-01-350000 for your test.

I've created a macro using xmlhttp requests to scrape the very content. It seems I've done everything in the right way but for some reason the macro is not working. It is still in the landing page even after making a post requests.

I've tried with:

Sub GetStatus()
    Const Url$ = "https://obftax.baltimorecountymd.gov/(S(m15cp5mubgqql1yzzjrxez45))/Default.aspx"
    Dim Html As New HTMLDocument
    Dim elem As Object, sVal$, payload As Variant
    
    sVal = "01-01-350000"

    payload = "RetryCounter=0&Action=MainMenu&ParcelType=RE&ParcelID=" & sVal & "&ParcelAddress=&PageNumber=1&SearchType=ParcelID&SearchParcel=" & sVal & "&SearchTaxNumber=&SearchStreetNumber=&SearchStreetName="

    With CreateObject("MSXML2.XMLHTTP")
        .Open "POST", Url, False
        .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .Send payload
        Html.body.innerHTML = .responseText
    End With
    
    Set elem = Html.querySelector("#tvrMessage")
    If Not elem Is Nothing Then
        MsgBox elem.innerText
    Else:
        MsgBox "failed to parse"
    End If
End Sub

How can I scrape the text from the yellow colored area using vba making use of xmlhttp requests?

标签: excelvbaweb-scrapingxmlhttprequest

解决方案


推荐阅读