我試圖從網站的靜態頁面中獲取一個數字,但是當我執行 HTML 請求時,結果是一個奇怪的 HTML,沒有我想要的原始 html 的資訊。
該網站,我試圖得到的資訊是: https://fnet.bmfbovespa.com.br/fnet/publico/exibirDocumento?id=233361&cvm=true 但我可以得到相同的結果: https://開頭FNET .bmfbovespa.com.br/fnet/publico/visualizarDocumento?id=233361&cvm=true
我想得到的數字是頁面中的數字“0,05”
我的代碼是:
Sub trying()
Dim html As HTMLDocument
Set html = New HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://fnet.bmfbovespa.com.br/fnet/publico/exibirDocumento?id=233361&cvm=true&", False
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" 'to deal with potential caching
.send
html.body.innerHTML = .responseText
End With
Set element = html.getElementsByTagName("td")(31).innerText
Sheets("Sheet1").Cells(1, 1) = element
End Sub
我也嘗試使用 InternetExplorer.Application 來做到這一點,但問題保持不變
uj5u.com熱心網友回復:
在嘗試了一堆請求頭后,Accept需要請求頭以 HTML 格式回傳回應:
Sub trying()
Dim html As HTMLDocument
Set html = New HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "http://fnet.bmfbovespa.com.br/fnet/publico/exibirDocumento?id=233361&cvm=true&", False
.setRequestHeader "Accept", "text/html,application/xhtml xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
.send
html.body.innerHTML = .responseText
End With
Dim element As String
element = html.getElementsByTagName("td")(32).innerText
Sheets("Sheet1").Cells(1, 1) = element
End Sub
uj5u.com熱心網友回復:
只需在請求中添加.htm(或.html)擴展名即可指定所需的檔案型別。
Option Explicit
Public Sub trying()
Dim html As MSHTML.HTMLDocument
Set html = New MSHTML.HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://fnet.bmfbovespa.com.br/fnet/publico/exibirDocumento.htm?id=233361", False
.send
html.body.innerHTML = .responseText
End With
Debug.Print html.querySelector("tr:nth-child(6) .dado-valores").innerText
End Sub
需要付出更多努力的替代方案包括.responseText在寫入之前添加接受標頭或 base64 解碼.innerHTML。
如果您使用了不太可取的 base64 解碼路由,使用這里的函式(注意:很確定這不是原始來源),那么您需要修改以下行:
.DataType = "bin.base64": .Text = Replace$(b64, Chr$(34), vbNullString) 'modified line
Public Function DecodeBase64(b64$)
Dim b
With CreateObject("Microsoft.XMLDOM").createElement("b64")
.DataType = "bin.base64": .Text = Replace$(b64, Chr$(34), vbNullString) 'modified line
b = .nodeTypedValue
With CreateObject("ADODB.Stream")
.Open: .Type = 1: .Write b: .Position = 0: .Type = 2: .Charset = "utf-8"
DecodeBase64 = .ReadText
.Close
End With
End With
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/371648.html
上一篇:如何將JSON解碼成不同的型別?
