【发布时间】:2021-10-24 14:24:27
【问题描述】:
Edit1:我的问题的解决方案可以在下面的 cmets 中找到,关于 Zwenn
在 excel 中打印时,我需要帮助来查找要在说明中使用的正确网站元素。以下是我在打印页面的前半部分时有效的内容,但不是第二部分。我无法使用下面的导航打印网页上的“全季课程统计”表格,我希望能够使用不同的元素导航来打印。
在网页上查找“all seasons course statistics”表格时,表格元素在检查网页时超过了“tabs-wrapper rns-scroll”类元素。
相关网站:https://www.racingandsports.com/thoroughbred/jockey/jake-bayliss/27461。
VBA代码的第一部分:
Sub Horse2()
Dim IE As InternetExplorer
Application.ScreenUpdating = False
Set IE = New InternetExplorer
IE.Visible = True
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim http As New XMLHTTP60, html As New HTMLDocument
Dim node As HTMLHtmlElement
Dim nodeTr As HTMLHtmlElement
Dim nodeDiv As HTMLHtmlElement
Dim Element1 As HTMLHtmlElement
Dim node1 As HTMLHtmlElement
Dim currentUrl As String
With http
.Open "GET", "https://www.racingandsports.com/thoroughbred/jockey/jake-bayliss/27461", False
.send
html.body.innerHTML = .responseText
End With
元素导航在 VBA 中从这里开始。这里的元素只是浏览其他表格而不是我需要的表格,我不知道如何让它进入“所有季节课程统计”表格,即使它包含相同的元素名称。
With html.getElementsByClassName("col-md-12 table-responsive")
For Each node In html.getElementsByClassName("table rns-table")
r = r + 1: c = 4
For Each nodeTr In node.getElementsByTagName("tr")
With nodeTr.getElementsByTagName("td")
If .Length Then
ws.Cells(r + 1, c + 3) = .Item(0).innerText
On Error Resume Next
ws.Cells(r + 1, c + 4) = .Item(1).innerText
On Error Resume Next
ws.Cells(r + 1, c + 5) = .Item(2).innerText
On Error Resume Next
ws.Cells(r + 1, c + 6) = .Item(3).innerText
On Error Resume Next
ws.Cells(r + 1, c + 7) = .Item(4).innerText
On Error Resume Next
ws.Cells(r + 1, c + 8) = .Item(5).innerText
On Error Resume Next
ws.Cells(r + 1, c + 9) = .Item(6).innerText
On Error Resume Next
ws.Cells(r + 1, c + 10) = .Item(7).innerText
On Error Resume Next
ws.Cells(r + 1, c + 11) = .Item(8).innerText
On Error Resume Next
ws.Cells(r + 1, c + 12) = .Item(9).innerText
On Error Resume Next
ws.Cells(r + 1, c + 13) = .Item(10).innerText
On Error Resume Next
ws.Cells(r + 1, c + 14) = .Item(11).innerText
On Error Resume Next
ws.Cells(r + 1, c + 15) = .Item(12).innerText
On Error Resume Next
r = r + 1
End If
End With
Next
Next
End With
IE.Quit
Set IE = Nothing
Application.StatusBar = ""
Application.ScreenUpdating = True
MsgBox "data input complete"
End Sub
【问题讨论】:
-
除了宏中有很多多余的代码和使用
On Error Resume Next的不太好的方法(顺便说一下,一次就足够了,但应该尽可能有选择地使用),HTML您加载的文档仅包含前 4 个表格。最后,您使用的是 xhr 而不是 IE。但是,xhr 只处理静态文档,它不能处理 JS 或连接多个文档。您可以在此 URL 下找到其他表格:racingandsports.com/Jockey/…