【发布时间】:2021-11-05 06:33:17
【问题描述】:
编辑:谢谢你们的解决方案,伙计们。
下面代码的问题是元素无法获取 div 类下的文本 "col-xs-12 col-sm-12 col-md-6 col-lg-5 col -md-pull-6 col-lg-pull-7 p-main-title-wrapper" 来自网页“https://www.racingandsports.com/thoroughbred/jockey/jake-bayliss/27461”和将其打印在 excel 的工作表上。唯一需要提取的文本是“JAKE BAYLISS”,仅此而已。
Sub Horse6()
Dim ws As Worksheet
Dim r As Integer
Dim c As Integer
Dim http As New XMLHTTP60
Dim html As New HTMLDocument
Dim node As HTMLHtmlElement
Dim nodeDiv As HTMLHtmlElement
Set ws = ThisWorkbook.Worksheets("Sheet1")
r = 2
c = 12
With http
.Open "GET", "https://www.racingandsports.com/thoroughbred/jockey/jake-bayliss/27461", False
.send
html.body.innerHTML = .responseText
End With
'Problems with the elements are here
With html.getElementsByClassName("np mainparent")
For Each node In html.getElementsByClassName("col-xs-12 col-sm-12 col-md-6 col-lg-5 col-md-pull-6 col-lg-pull-7 p-main-title-wrapper")
For Each nodeDiv In node.getElementsByTagName("div")
ws.Cells(r, c) = .Item(0).innerText
Next
Next
End With
MsgBox "Data input complete"
End Sub
【问题讨论】: