简体   繁体   English

VBA 抓取包含在 Javascript 中的 Web 数据

[英]VBA scrape web data which is contained in Javascript

I'm trying to get web table data from the following below website and extract the first table regarding Policy rates on the right hand-side.我正在尝试从以下网站获取网络表格数据,并在右侧提取有关政策利率的第一个表格。

https://www.researchonline.se/macro/our_forecasts https://www.researchonline.se/macro/our_forecasts

Have use the following code just to see if it spits out the desired data but keep getting error 91. Suspecting something about Javascript that I need to consider in my code?使用以下代码只是为了查看它是否会吐出所需的数据,但不断收到错误 91。怀疑我需要在代码中考虑的有关 Javascript 的某些内容? Below is my code.下面是我的代码。

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://www.researchonline.se/macro/our_forecasts"

' 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 variable from the specified element on the page and just check in a message box if that is the correct data.

price = html.getElementsByTagName("table")(0).innerText
' Output the price into a message box.
MsgBox price
Set request = CreateObject("MSXML2.XMLHTTP")

The above cannot execute javascript , Though instead of calling url and fetching it from table.上面不能执行 javascript ,虽然不是调用 url 并从表中获取它。 you can directly use their API你可以直接使用他们的 API

for example to get policy rate, the site uses API (There are many apis , i have seen by looking into the site).例如为了获得政策费率,该网站使用 API(有很多 api,我通过查看该网站已经看到)。 One example is一个例子是

https://www.researchonline.se/api/MacroAdmin/GetForecast?name=policy&start=Mon%20Sep%2014%202020&end=Tue%20Sep%2014%202021 https://www.researchonline.se/api/MacroAdmin/GetForecast?name=policy&start=Mon%20Sep%2014%202020&end=Tue%20Sep%2014%202021

which will return an xml kindda response, which you can parse with MSXML2.XMLHTTP它将返回一个 xml kindda 响应,您可以使用 MSXML2.XMLHTTP 解析它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM