如何在VBA中单击网页按钮进行解析 [英] How to click a webpage button in VBA for parsing

查看:109
本文介绍了如何在VBA中单击网页按钮进行解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to access all the information on the following table:

https://www.tradingview.com/markets/股票-美国/sectorandindustry-industry/生物技术/

使用以下 VBA 代码:

using the following VBA code:

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

这很好用,但无法获取页面上的所有内容.在页面底部,有一个按钮,您必须单击才能加载更多"信息.我认为按钮定义如下:

This works great but isn't capable of getting everything on the page. On the bottom of the page, there is a button you must click to "Load More" information. I think the button is defined below:

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

我尝试了几种不同的方法,但还没有成功.有没有办法让 VBA 自动化以一直单击该按钮直到所有内容都加载完毕,然后运行上面的 VBA 代码?

I've tried a few different methods but haven't found success yet. Is there a way to automate VBA to keep clicking that button until everything is loaded, then run the VBA code above?

推荐答案

这是一些使用 Selenium 的代码.唯一的问题是它比原始代码慢得多......我想我会继续寻找其他数据源.再次,非常感谢任何建议!

Here's some code using Selenium. The only problem is that it's much slower than the original code... I think I'll continue looking for other data sources. Once again, any suggestions are much appreciated!

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

这篇关于如何在VBA中单击网页按钮进行解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆