我从此youtube教程中复制了一个代码,以便从hsbc衍生产品中获取股票数据。 (https://www.youtube.com/watch?v=IOzHacoP-u4)
我只是更改了URL(更改为hsbc),然后更改了我想根据ID(而不是类名&)找到值的方法,当然我也更改了ID名称。 当我运行代码时,我立即(因此我想Excel不会进行爬网)得到该错误:“运行时错误91:对象变量或未设置块变量”。 谁可以帮助我解决此问题? 我是一个初学者,会非常感激。 预先感谢
最好。
Sub Get_Web_Data()
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.hsbc-zertifikate.de/home/details#!/isin:DE000TR8S293"
' 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.getElementById("kursdaten20").innerText
' Output the price into a message box.
MsgBox price
End Sub
答案 0 :(得分:0)
您正在搜索页面上不存在的元素ID kursdaten20
。
html.getElementById("kursdaten20")
返回Nothing
,您正在使用innerText
引用访问Nothing/Null
属性。
搜索元素时,可以添加一个检查元素是否存在:
'query the document
Set element = html.getElementById("kursdaten20")
If Not element Is Nothing Then
' Get the price from the specified element on the page.
price = element.innerText
' Output the price into a message box.
MsgBox price
Else
' no price
MsgBox "no price"
End If
答案 1 :(得分:0)
恐怕它比您预期的要复杂。
我假设您关注的信息是这样:
Geldkurs(1卡住)4,01 EUR
Briefkurs(1卡住)4,11 EUR
这些字段不是静态的。它们由脚本动态更新(我想每当进行交易)。这就是为什么您无法在HTML
页面的源代码中找到其ID的原因。
但是,有一种方法可以通过在每次更新这些字段时复制发送到服务器的HTTP request
来获取所需的信息。
要找到此请求及其参数,您需要在使用浏览器的开发人员工具加载页面时检查网络流量。
此请求返回一个(结构很差的IMHO)JSON
响应,其中包含另一个JSON
(!!),其中包含您想要的信息,格式为HTML
(!!)。第二个JSON
如下所示:
更糟糕的是,您在state
下看到的名称会随着您发送的每个请求而更改。
因此,首先您需要解析json
响应。然后,您需要在初始json
响应中解析json
,以获取HTML
代码。然后,使用HTML文档对象,您可以轻松访问包含所需信息的HTML
表。
这是这样做的方法:
Option Explicit
Sub hsbc()
Dim req As New WinHttpRequest
Dim doc As New HTMLDocument
Dim table As HTMLTable
Dim cell As HTMLTableCell
Dim parsedJSON As Object
Dim key As Variant
Dim htmlCode As String
Dim url As String, reqBody As String, resp As String
url = "https://www.hsbc-zertifikate.de/web-htde-tip-zertifikate-main/?components=YW1wZWw6UnRQdWxsQ29tcG9uZW50KCdhbmltQ3NzLGMtaGlnaGxpZ2h0LXVwLGMtaGlnaGxpZ2h0LWRvd24sYy1oaWdobGlnaHQtY2hhbmdlZCcpO3NlYXJjaGhpbnRfbW9iaWxlOlNlYXJjaEhpbnRNb2JpbGVDb21wb25lbnQoJ3VsU2VhcmNoU21hbGwvc2VhcmNoSW5wdXRNb2JpbGUnKTtzZWFyY2hoaW50OlNlYXJjaEhpbnRDb21wb25lbnQoJ3VsU2VhcmNoRnVsbC9zZWFyY2gtaGVhZGVyJyk7aXNpbjpSZXNwb25zaXZlU25hcHNob3RDb21wb25lbnQoJ2ZhbHNlJyk%3D&pagepath=https%3A%2F%2Fwww.hsbc-zertifikate.de%2Fhome%2Fdetails%23!%2Fisin%3ADE000TR8S293&magnoliaSessionId=B22F70D76986AB6BACDF110E4E7A724C.public7a&v-1566551332455"
reqBody = "v-browserDetails=1&theme=hsbc&v-appId=myApp&v-sh=1080&v-sw=1920&v-cw=1920&v-ch=550&v-curdate=1566551332455&v-tzo=-180&v-dstd=60&v-rtzo=-120&v-dston=true&v-vw=50&v-vh=50&v-loc=https%3A%2F%2Fwww.hsbc-zertifikate.de%2Fhome%2Fdetails%23!%2Fisin%3ADE000TR8S293&v-wn=myApp-0.5436432044490654"
With req
.Open "POST", url, False
.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
.send reqBody
resp = .responseText
End With
Set parsedJSON = JsonConverter.ParseJson(resp)
Set parsedJSON = JsonConverter.ParseJson(parsedJSON("uidl"))
For Each key In parsedJSON("state").Keys
If parsedJSON("state")(key)("contentMode") = "HTML" Then
htmlCode = htmlCode & parsedJSON("state")(key)("text")
End If
Next key
doc.body.innerHTML = htmlCode
Set table = doc.getElementsByTagName("table")(0)
Debug.Print table.Rows(2).innerText
Debug.Print table.Rows(3).innerText
End Sub
出于演示目的,结果将显示在您的直接窗口中。
您将需要在项目中添加以下引用(VBE>工具>参考):
您还需要将this JSON parser添加到您的项目中。请按照链接中的安装说明进行操作,然后就可以开始使用了。