刮擦TSP网站时,我可以使用Excel VBA HTML刮擦访问股票价格表中的数据

发布于 2025-02-04 10:02:25 字数 1516 浏览 2 评论 0原文

我正在抓取网站“ https://www.tsp.gov/fund-performance/share-price-history/”,如果我使用getElementById(“ dynamic-share-share-div”),我可以debug.print nistarhtml但是,如果我使用靠近我想要的桌子的getElementById(“ Dynamic-Share-Price-table”)。当它们都具有唯一的DIV ID时,为什么我可以将InnerHTML打印为一个而不是另一个。我只是不知道如何到达Div中的唯一表格。我尝试在返回的每个div对象上使用getElementsbytagname(“ table”)(0)(0),但我一无所获。当我在DIV ID上使用getElementById直接转到表(上面的第二个唯一Div ID)时,我什至无法调试。这是我的代码。

sub tspxmlget()

Dim req As New MSXML2.XMLHTTP60
Dim reqURL As String
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim HTMLDiv As MSHTML.IHTMLElement
Dim HTMLTable As MSHTML.IHTMLElement

reqURL = "https://www.tsp.gov/fund-performance/share-price-history/"

req.Open "GET", reqURL, False

req.send

If req.Status <> 200 Then
    MsgBox req.Status & " - " & req.StatusText
    Exit Sub
End If

HTMLDoc.body.innerHTML = req.responseText

Set HTMLDiv = HTMLDoc.getElementById("dynamic-share-price-div")

'Set HTMLDiv = HTMLDoc.getElementById("dynamic-share-price-table") 'This one shows that Div element has been populated

Debug.Print HTMLDiv.innerHTML 'Will get print in immediate window only for first choice (-div but not -table)

'Set HTMLDiv = HTMLDoc.getElementsByTagName("table")(0)

Set HTMLTable = HTMLDiv.getElementsByTagName("table")(0)  ' Says object required

Debug.Print HTMLTable.innerHTML  ' get nothing in immediate window since no object

结束

表主体的xpath是:“/html/body/main/main/div/div [2]/div/div/div/div/div/div/div/div [2]/table/tbody”知道如何编码XPath

I am scraping the website "https://www.tsp.gov/fund-performance/share-price-history/" and I can debug.print the innerHTML if I use getElementbyId("dynamic-share-price-div") but not if I use getElementbyId("dynamic-share-price-table") which is closer to the table I want. Why can I print the innerHTML for one but not the other when they both have unique div Id. I just can't figure out how to get to the only table in the div. I have tried using getElementsbyTagName("table")(0) on each of the Div objects returned but I get nothing. When I use the getElementbyID on the div id going directly to the table (2nd unique div id above), I can't even debug.print the innerHTML. Here is my code.

Sub TSPxmlGet()

Dim req As New MSXML2.XMLHTTP60
Dim reqURL As String
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim HTMLDiv As MSHTML.IHTMLElement
Dim HTMLTable As MSHTML.IHTMLElement

reqURL = "https://www.tsp.gov/fund-performance/share-price-history/"

req.Open "GET", reqURL, False

req.send

If req.Status <> 200 Then
    MsgBox req.Status & " - " & req.StatusText
    Exit Sub
End If

HTMLDoc.body.innerHTML = req.responseText

Set HTMLDiv = HTMLDoc.getElementById("dynamic-share-price-div")

'Set HTMLDiv = HTMLDoc.getElementById("dynamic-share-price-table") 'This one shows that Div element has been populated

Debug.Print HTMLDiv.innerHTML 'Will get print in immediate window only for first choice (-div but not -table)

'Set HTMLDiv = HTMLDoc.getElementsByTagName("table")(0)

Set HTMLTable = HTMLDiv.getElementsByTagName("table")(0)  ' Says object required

Debug.Print HTMLTable.innerHTML  ' get nothing in immediate window since no object

End Sub

The Xpath of the table body is: "/html/body/main/div/div[2]/div/section[1]/div/div[2]/table/tbody" but I don't know how to code the Xpath

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文