VBA DOM getElementsBy无法获得子节点

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

我正在尝试获取标签的内文,但出现错误。通过控制台,我成功使用此脚本获取内部文本:

document.getElementsByClassName("item alt")[0].childNodes[2].childNodes[0].innerText

我正在尝试获取的元素:

<tr class="item alt" data-id="1376936"><td class="toolbar left"><a href="#" class="show-incidents button-small ui-state-transparent-default rc" title="Details"><span class="ui-icon ui-icon-triangle-1-e"></span></a></td><td class="time">14:00</td><td class="status"><span class="status-1 rc">FT</span>

我的VBA脚本:

Sub WebScraping()
    Dim ie As InternetExplorer
    Dim html As HTMLDocument
    Set ie = New InternetExplorer


    ie.Visible = True

    ie.navigate "https://www.whoscored.com/Regions/74/Tournaments/22/Seasons/7814/Stages/17593/Fixtures/France-Ligue-1-2019-2020"

    Do While ie.readyState <> READYSTATE_COMPLETE
    Application.StatusBar = "Trying to go to Whoscored ..."
    DoEvents
    Loop

    Set doc = ie.document


    Do While ie.readyState <> READYSTATE_COMPLETE
    Application.StatusBar = "Trying to go to Whoscored ..."
    DoEvents
    Loop

    Set a = doc.getElementsByClassName("item alt")(0).ChildNodes(2).ChildNodes(0).innerText
    MsgBox (a)

End Sub
excel vba dom web-scraping child-nodes
1个回答
0
投票
Set a = doc.getElementsByClassName("item alt")(0).ChildNodes(2).ChildNodes(0).innerText

尝试使用getElementsByClasssName方法查找子节点,请按如下所示修改上面的代码:

Dim a As String   
a = doc.getElementsByClassName("item alt")(0).getElementsByClassName("status")(0).getElementsByClassName("status-1")(0).innerText

MsgBox (a)
© www.soinside.com 2019 - 2024. All rights reserved.