【问题标题】:VBA scraping web table within form post divVBA 在表单发布 div 中抓取网页表
【发布时间】:2020-12-27 19:43:12
【问题描述】:

我正在尝试从以下网站获取网络表格数据并提取掉期利率。

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

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

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

【问题讨论】:

    标签: html vba web-scraping scrape getelementsbytagname


    【解决方案1】:

    数据位于iframe 中。使用 src 属性的值来请求 iframe 内容:

    https://seb.se/pow/apps/swaprates/default.aspx
    

    一旦您拥有iframe 内容,在撰写本文时,就有 10 个表与以下 css 选择器列表匹配:#doc table

    这意味着您可以使用html.querySelectorAll("#doc table") 来检索所有必需的表。

    要访问单个表,请使用以下循环:

    For i = 0 to html.querySelectorAll("#doc table").Length -1

    ,其中iLong,并使用以下命令访问循环中的每个表:

    html.querySelectorAll("#doc table")item(i)

    为了提高效率,首先将querySelectorAll返回的nodeList存储到一个变量中,然后如上所述循环.Length


    上述更传统的语法版本是:

    html.getElementById("doc").getElementsByTagName("table")

    然后For Each 覆盖返回的表。


    旁注:

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

    【讨论】:

    • 这正是我正在寻找的答案,iframe 规范未被捕获。一周以来一直试图找出导致此错误 91 的原因,非常感谢您的快速输入!
    • 实际上,如果我可以补充的话,这是一个后续问题。如何获取网页researchonline.se/macro/our_forecasts 表格中的“保单费率”?似乎这里没有 iframe 问题,并且仍然再次出现相同的错误。提前致谢
    • 您需要提出一个新问题,分享您的代码并解释什么不起作用。
    • 好的,谢谢,将在新线程中作为新问题发布。
    猜你喜欢
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 2019-12-17
    • 2015-09-17
    相关资源
    最近更新 更多