我试图写一些从HTML元素中提取.innerText
的东西。这是我的适用代码:
Sub test()
Dim URL = "https:// ** the website **"
Dim TDelements As IHTMLElementCollection, TDElementsRef As IHTMLElementCollection
Set IE = New InternetExplorer
With IE
.Navigate URL
.Visible = True
' Wait for the page to load
While .Busy Or .READYSTATE <> 4: DoEvents: Wend
Set HTMLdoc = .Document
End With
Set TDelements = HTMLdoc.getElementsByTagName("td")
Set TDElementsRef = HTMLdoc.getElementsByClassName("tdData")
r = 0
For Each TDelement In TDElementsRef
If bGetNext Then
Range("A1").Offset(r - 1, 1).Value = TDelement.innerText
bGetNext = False
End If
Debug.Print TDelement.innerText
If TDelement.className = "tdTitle" Then
If TDelement Then
Range("A1").Offset(r + 1, 0).Value = TDelement(2).innerText
bGetNext = True 'Trigger to get the information from the next TDelement
r = r + 1
End If
End If
Next
End sub
这将遍历HTML中<td>
标记之间的所有信息。但是,我怎么问VB我目前的INDEX?我尝试过(仅在两条Debug.Print
行之后)Debug.Print TDelement.Index
,但当然这不对。我如何获得每个TDelement的索引?
HTML看起来像这样:
我只需要获取绿色信息。
答案 0 :(得分:2)
您已将TDelementsRef
声明为集合,但您尝试将其设置为单个元素。
如果您只想获得单个特定td元素的值,那么:
Dim tdVal as String
tdVal = Document.getElementByClassName("tdData")(3).innerText
应该做你需要的。