0

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

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
4

2 回答 2

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)
于 2020-05-21T11:30:48.640 回答
0

每个模块的第一行应该是Option Explicit.

我完全不确定你想要什么。但是要显示想要的元素,请使用:

Sub WebScraping()
  Dim ie As InternetExplorer
  Dim doc As HTMLDocument
  Dim a As Object
  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
  Application.StatusBar = False

  Set doc = ie.document

  Set a = doc.getElementsByClassName("item alt")(0).getElementsByClassName("status")(0)
  MsgBox a.innerText
End Sub
于 2020-05-22T13:30:37.047 回答