gpt4 book ai didi

vba - excel VBA中的网页抓取

转载 作者:行者123 更新时间:2023-12-04 21:57:51 25 4
gpt4 key购买 nike

我试图从网上抓取一张 table ,但由于某种原因我没有得到整个 table 。它只获取 1 列而不是全部。任何帮助将不胜感激!谢谢!

这是我的代码:

Sub HistoricalData()

Dim xmlHttp As Object
Dim TR_col As Object, TR As Object
Dim TD_col As Object, TD As Object
Dim row As Long, col As Long

Set xmlHttp = CreateObject("MSXML2.XMLHTTP.6.0")
xmlHttp.Open "GET", "http://www.cnbc.com/bonds-canada-treasurys", False
xmlHttp.setRequestHeader "Content-Type", "text/xml"
xmlHttp.send

Dim html As Object
Set html = CreateObject("htmlfile")
html.body.innerHTML = xmlHttp.responseText

Dim tbl As Object
Set tbl = html.getElementById("curr_table")

row = 1
col = 1

Set TR_col = html.getElementsByTagName("TR")
For Each TR In TR_col
Set TD_col = TR.getElementsByTagName("TD")
For Each TD In TD_col
Cells(row, col) = TD.innerText
col = col + 1
Next
col = 1
row = row + 1
Next
End Sub

最佳答案

问题是你得到了 HTTP.responseText在页面完成加载之前返回。

我无法获得 MSXML2.XMLHTTP.6.0在返回 HTTP.responseText 之前等待页面完成加载,所以我切换到IE .

enter image description here

Sub HistoricalData()
Const URL As String = "http://www.cnbc.com/bonds-canada-treasurys"
Const READYSTATE_COMPLETE As Integer = 4
Dim IE As Object
Dim TR_col As Object, TR As Object
Dim TD_col As Object, TD As Object
Dim row As Long, col As Long

Set IE = CreateObject("InternetExplorer.Application")

IE.Navigate URL

Do While (IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE)
DoEvents
Loop

Set TR_col = IE.Document.getElementsByTagName("TR")

For Each TR In TR_col
Set TD_col = TR.getElementsByTagName("TD")

For Each TD In TD_col
Cells(row, col) = TD.innerText
col = col + 1
Next
col = 1
row = row + 1
Next
End Sub

关于vba - excel VBA中的网页抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40999168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com