表格后div中的VBA抓取网页表

问题描述

我正试图从下面的以下网站获取Web表格数据,并提取掉期利率。

https://sebgroup.com/large-corporates-and-institutions/prospectuses-and-downloads/rates/swap-rates

使用以下代码只是为了查看它是否吐出了所需的数据,但始终出现错误91。在get元素规范中我缺少什么?下面是我的代码

Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim website As String
Dim price As Variant

' Website to go to.
website = "https://sebgroup.com/large-corporates-and-institutions/prospectuses-and-downloads/rates/swap-rates"

' Create the object that will make the webpage request.
Set request = CreateObject("MSXML2.XMLHTTP")

' Where to go and how to go there - probably don't need to change this.
request.Open "GET",website,False

' Get fresh data.
request.setRequestHeader "If-Modified-Since","Sat,1 Jan 2000 00:00:00 GMT"

' Send the request for the webpage.
request.send

' Get the webpage response data into a variable.
response = StrConv(request.responseBody,vbUnicode)

' Put the webpage into an html object to make data references easier.
html.body.innerHTML = response

' Get the price from the specified element on the page.

price = html.getElementsByTagName("table")(0).innerText
' Output the price into a message Box.
MsgBox price

解决方法

数据在iframe中。向

的iframe src发出请求
https://seb.se/pow/apps/swaprates/default.aspx

假设允许抓取。

然后有10个表作为CSS选择器:#doc表

意味着您可以使用html.getElementById("doc").getElementsByTagName("table")For Each遍历检索到的表,或使用html.querySelectorAll("#doc table")For i = 0 to html.querySelectorAll("#doc table").Length -1循环并使用html.querySelectorAll("#doc table")item(i)循环访问每个表 得到他们所有。 i As Long。最好将结果放入变量并循环引用。

您可能只需将xhr请求的.innerText响应分配给html.body.innerHTML,而无需StrConv