如何在VBA中单击网页按钮进行解析

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

我正在尝试访问下表中的所有信息:

https://www.tradingview.com/markets/stocks-usa/sectorandindustry-industry/biotechnology/

使用以下VBA代码:

Sub GetInfo()

    Dim XMLPage As New MSXML2.XMLHTTP60
    Dim HTMLDoc As New MSHTML.HTMLDocument
    Dim HTMLTable, HTMLRow, HTMLCell, button As MSHTML.IHTMLElement

    XMLPage.Open "GET", "https://www.tradingview.com/markets/stocks-usa/sectorandindustry-industry/biotechnology/", False
    XMLPage.send

    HTMLDoc.body.innerHTML = XMLPage.responseText

    Set HTMLTable = HTMLDoc.getElementsByTagName("tr")

    For Each HTMLRow In HTMLTable
        For Each HTMLCell In HTMLRow.Children
            Debug.Print HTMLCell.innerText
        Next HTMLCell
    Next HTMLRow

End Sub

这很好用,但无法在页面上显示所有内容。在页面底部,必须单击一个按钮才能“加载更多”信息。我认为按钮定义如下:

<div class="tv-load-more tv-load-more--screener js-screener-load-more">
    <span class="tv-load-more__btn">Load More</span>

我尝试了几种不同的方法,但尚未找到成功。有没有一种方法可以自动使VBA保持单击该按钮,直到所有内容加载完毕,然后运行上面的VBA代码?

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

以下是一些使用Selenium的代码。唯一的问题是,它比原始代码要慢得多。我想我将继续寻找其他数据源。再次感谢您提出任何建议!

Sub GetTickerInfo()

    Dim driver As New Selenium.ChromeDriver
    Dim click As Boolean

    With driver
        .AddArgument "--headless"
        .Get "https://www.tradingview.com/markets/stocks-usa/sectorandindustry-industry/biotechnology/"
    End With

    click = True

    On Error GoTo done
    While click
        driver.FindElementByClass("tv-load-more__btn").click
        Application.Wait Now + TimeValue("00:00:01")
    Wend

done:
    Resume continue
continue:

    For Each tr In driver.FindElementsByTag("tr")
       For Each td In tr.FindElementsByTag("td")
           Debug.Print td.Text
       Next td
    Next tr

End Sub
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.