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

问题描述 投票:0回答:1

我想在excel工作表中从Yahoo Finance筛选一些股票的价格。

我的方法是使用:

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"

谢谢

excel vba web-scraping
1个回答
0
投票

您可以尝试使用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
© www.soinside.com 2019 - 2024. All rights reserved.