我正在尝试基于“ 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
如前所述,我希望能够在设置此解决方案时使用后期绑定。
答案 0 :(得分:0)
如@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
尝试正则表达式here