我正在尝试建立基于“https://finance.yahoo.com”的股票价值自动更新。
我需要使用后期绑定,这是行不通的,而早期绑定工作得很好。有办法解决吗?
Sub FetchFinanceInfoLateBinding()
Dim XMLReq As Object
Dim HTMLDoc As Object
Dim post As Object, I&
Set XMLReq = CreateObject("Msxml2.ServerXMLHTTP.6.0")
'Set HTMLDoc = CreateObject("MSXML2.DOMDocument.6.0")
Set HTMLDoc = CreateObject("htmlfile")
XMLReq.Open "GET", "https://finance.yahoo.com/quote/BABA/cash-flow?p=BABA", False
XMLReq.send
HTMLDoc.body.innerHTML = XMLReq.responseText
Set post = HTMLDoc.getElementsByClassName("Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)")(0)
Debug.Print post.innerText
End Sub
Sub FetchFinanceInfoEarlyBinding()
Dim XMLReq As New XMLHTTP60
Dim HTMLDoc As New HTMLDocument
Dim post As Object, I&
XMLReq.Open "GET", "https://finance.yahoo.com/quote/BABA/cash-flow?p=BABA", False
XMLReq.send
HTMLDoc.body.innerHTML = XMLReq.responseText
Set post = HTMLDoc.getElementsByClassName("Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)")(0)
Debug.Print post.innerText
End Sub
如前所述,我希望在设置此解决方案时能够使用后期绑定。
最佳答案
正如@omegastripes评论中提到的:
后期绑定的htmlfile使用低于9的IE版本,这不支持
.getElementsByClassName方法
.
不过,您可以将其正则化:
Option Explicit
Public Sub FetchFinanceInfoLateBinding()
Dim XMLReq As Object
Dim HTMLDoc As Object
Dim post As Object, I&
Set XMLReq = CreateObject("Msxml2.ServerXMLHTTP.6.0")
Set HTMLDoc = CreateObject("htmlfile")
XMLReq.Open "GET", "https://finance.yahoo.com/quote/BABA/cash-flow?p=BABA", False
XMLReq.send
HTMLDoc.body.innerHTML = XMLReq.responseText
MsgBox GetValue(XMLReq.responseText, """regularMarketPrice"":{""raw"":[0-9.]+,""fmt"":""(\d+\.\d+)""}")
End Sub
Public Function GetValue(ByVal inputString As String, ByVal sPattern As String) As String
With CreateObject("vbscript.regexp")
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = sPattern
If .test(inputString) Then
GetValue = .Execute(inputString).item(10).SubMatches(0)
Else
GetValue = vbNullString
End If
End With
End Function
尝试regexhere