通过使用VBA进行屏幕抓取获取自定义元素



我想在我的excel表中从雅虎财经中筛选一些股票的价格。

我的方法是使用:

Function Scrape()
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate "https://de.finance.yahoo.com/quote/TSLA"
.Visible = True
End With
Do While appIE.Busy
DoEvents
Loop
Set data = appIE.document.getElementById("data-reactid") #this is the point where I'm stuck
End Function

我的问题是如何获取自定义元素,例如:

<span class="Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" data-reactid="32">1.025,05</span>

该网站似乎为每个元素使用reactid,这使得查明元素变得容易。对于上面的示例,我将如何做到这一点data-reactid="32"

谢谢

您可以尝试使用 xhr,因为您要查找的内容在页面源代码中可用。这是您可以采取的有效方法之一:

Public Sub GetPrice()
Const Url$ = "https://de.finance.yahoo.com/quote/TSLA"
Dim S$, itemPrice$
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", Url, False
.send
S = .responseText
End With
With CreateObject("HTMLFile")
.write S
itemPrice = .getElementById("quote-market-notice").ParentNode.FirstChild.innerText
MsgBox itemPrice
End With
End Sub

最新更新