我正在嘗試使用下面的代碼使用 XMLHTTP 捕獲對 POST 請求的回應
Dim XMLPage As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim htmlEle1 As MSHTML.IHTMLElement
Dim htmlEle2 As MSHTML.IHTMLElement
Dim URL As String
Dim elemValue As String
URL = "https://www.informadb.pt/pt/pesquisa/?search=500004064"
XMLPage.Open "GET", URL, False
XMLPage.send
HTMLDoc.body.innerHTML = XMLPage.responseText
For Each htmlEle1 In HTMLDoc.getElementsByTagName("div")
Debug.Print htmlEle1.className
If htmlEle1.className = "styles__SCFileModuleFooter-e6rbca-1 kUUNkj" Then
elemValue = Trim(htmlEle1.innerText)
If InStr(UCase$(elemValue), "CONSTITU") > 0 Then
'Found Value
Exit For
End If
End If
Next htmlEle1
問題是我找不到類名“styles__SCFileModuleFooter-e6rbca-1 kUUNkj”,因為我注意到當我在 URL 的搜索框中手動插入值 (500004064) 時:https ://www.informadb.pt /pt/pesquisa/,網頁生成附加流量并出現在端點 URL :https://www.informadb.pt/pt/pesquisa/empresa/?Duns=453060832,該類名可以在請求回應文本。
我的目標是使用第一個 URL 來檢索 Duns 編號“453060832”,以便能夠訪問 EndPoint URL 的 ResponseText 中的資訊。為了捕獲 Duns Number,我需要找到一種方法來獲取 Endpoint URL,或者嘗試獲取下面的 POST 請求回應,并使用 JSON 決議器獲取該值:
{'TotalResults': 1,
'NumberOfPages': 1,
'Results': [{'Duns': '453060832',
'Vat': '500004064',
'Name': 'A PANIFICADORA CENTRAL EBORENSE, S.A.',
'Address': 'BAIRRO DE NOSSA SENHORA DO CARMO,',
'Locality': 'éVORA',
'OfficeType': 'HeadOffice',
'FoundIn': None,
'Score': 231.72766,
'PageUrl': '/pt/pesquisa/empresa/?Duns=453060832'}]}
我無法使用 XMLHTTP Browser 請求捕獲實際發生的情況,這似乎是以下步驟:
導航到https://www.informadb.pt/pt/pesquisa/?search=500004064
網頁產生額外的流量
其中額外的流量是一個 API POST XHR 請求,它以 JSON 形式回傳搜索結果。該請求發送至 https://www.informadb.pt/Umbraco/Api/Search/Companies,并在帖子正文的引數中包含 500004064 識別符號
根據 API 結果,瀏覽器最終到達以下 URI https://www.informadb.pt/pt/pesquisa/empresa/?Duns=453060832
有人可以幫我嗎,我必須使用VBA來做。提前致謝。
uj5u.com熱心網友回復:
一個小例子,如何使用 VBA 將資料發布到您的網站,以及如何使用準系統字串處理從結果中提取資料,如我上面的評論中所述。
Function GetVatId(dunsNumber As String) As String
With New MSXML2.XMLHTTP60
.open "POST", "https://www.informadb.pt/Umbraco/Api/Search/Companies", False
.setRequestHeader "Content-Type", "application/json"
.send "{""Page"":0,""PageSize"":5,""SearchTerm"":""" & dunsNumber & """,""Filters"":[{""Key"":""districtFilter"",""Name"":""Distrito"",""Values"":[]},{""Key"":""legalFormFilter"",""Name"":""Forma Jurídica"",""Values"":[]}],""Culture"":""pt""}"
If .status = 200 Then
MsgBox "Response: " & .responseText, vbInformation
GetVatId = Mid(.responseText, InStr(.responseText, """Vat"":""") 7, 9)
Else
MsgBox "Repsonse status " & .status, vbExclamation
End If
End With
End Function
用法:
Dim vatId As String
vatId = GetVatId("453060832") ' => "500004064"
對于更強大的解決方案,您應該使用 JSON 決議器和 -serializer,例如https://github.com/VBA-tools/VBA-JSON。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/430507.html
