[英]VBA data-reactid for Yahoo Finance's financial numbers
I googled data-reactid and it seems it won't work with VBA in most cases. 我在data-reactid上搜索了一下,看来在大多数情况下它不适用于VBA。
But I still think there maybe a possibility to get an answer. 但是我仍然认为可能会有答案。 I want to get numbers in " Total Cash Flow From Operating Activities "
我想从“经营活动产生的总现金流量 ”中获取数字
https://finance.yahoo.com/quote/BABA/cash-flow?p=BABA https://finance.yahoo.com/quote/BABA/cash-flow?p=BABA
Sub YFinance()
Dim XMLReq As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim i As Integer
Dim strUrl As String
XMLReq.Open "GET", "https://finance.yahoo.com/quote/BABA/cash-flow?p=BABA", False
XMLReq.send
If XMLReq.Status <> 200 Then
MsgBox "Error!"
Exit Sub
End If
HTMLDoc.body.innerHTML = XMLReq.responseText
Set XMLReq = Nothing
MsgBox HTMLDoc.getElementsById("Bdbw(0px)! H(36px)")(0).innerText
End Sub
getElementById
returns a single node so you wouldn't index into it as you are doing. getElementById
返回单个节点,因此您不会在执行操作时对其进行索引。 There is no method getElementsById
so that should fail with an error. 没有方法
getElementsById
因此应该失败并显示错误。
You could use the class and escape the special characters and index into returned collection 您可以使用该类并转义特殊字符并索引到返回的集合中
MsgBox HTMLDoc.querySelectorAll(".Bdbw\(0px\)\!")(1).innerText
Or pass the compound class to getElementsByClassName
: 或将复合类传递给
getElementsByClassName
:
MsgBox HTMLDoc.getElementsByClassName("Bdbw(0px)! H(36px)")(1).innerText
The attribute (not id) data-reactid
can vary between page and output (as it does in this case - the id is 113 for the row in the output. In this instance it may be safer to use table and row indices data-reactid
的属性 (非id)在页面和输出之间可能会有所不同(在这种情况下-输出中的行的id为113。在这种情况下,使用表和行索引可能更安全
MsgBox HTMLDoc.getElementsByTagName("table")(2).getElementsByTagName("tr")(9).innerText
If you want column by column for that row: 如果您希望该行逐列:
Dim td As Object, tds As Object
Set tds = HTMLDoc.getElementsByTagName("table")(2).getElementsByTagName("tr")(9).getElementsByTagName("td")
For Each td In tds
Debug.Print td.innerText
Next
Or 要么
Dim td As Object, tds As Object
Set tds = HTMLDoc.getElementsByClassName("Bdbw(0px)! H(36px)")(1).getElementsByTagName("td")
For Each td In tds
Debug.Print td.innerText
Next
As I said, the data-reactid can flex but if you want to know how to apply an attribute selector for this, see: 正如我所说,数据反应堆可以伸缩,但是如果您想知道如何为此应用属性选择器,请参阅:
MsgBox HTMLDoc.querySelector("tr[data-reactid='113']").innerText
You can try the following to play safe. 您可以尝试以下方法以确保安全。 Once the
for loop
gets the required node, it will fetch you the desired content and exit the loop. 一旦
for loop
获得所需的节点,它将获取您所需的内容并退出循环。
Sub FetchFinanceInfo()
Dim XMLReq As New XMLHTTP60, 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
For Each post In HTMLDoc.getElementsByTagName("span")
If InStr(post.innerText, "From Operating Activities") > 0 Then
With post.ParentNode.ParentNode.getElementsByTagName("td")
For I = 1 To .Length - 1
Debug.Print .Item(I).innerText
Next I
End With
Exit For
End If
Next post
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.