我有一个像这样的HTML表格:
<table class="Example">
<tr>
<th>Header1</th>
<td>Value1></td>
</tr>
<tr>
<th>Header2</th>
<td>Value2></td>
</tr>
</table>
我想找到<th>
等于“Header2”,然后返回相应的<td>
,即“Value2”,它位于同一<tr>
内。
我知道我可以轻松使用索引号,例如getElementsByTagName("td")(1)
找到这个值,但这是不可行的,因为每个页面可能会混淆行。
我尝试过这种不同的方式而没有成功。希望以下代码表明我正在尝试做什么:
Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
Sub WebSearch()
Dim URL As String
Dim IE As Object
Dim HWNDSrc As Long
Dim html As IHTMLDocument
Dim Example As IHTMLElement
Dim TableRows As IHTMLElementCollection
Dim TableRow As IHTMLElement
Dim RowChildren As IHTMLElementCollection
Dim RowChild As IHTMLElement
Dim TableHeader As IHTMLElement
Dim TableData As IHTMLElement
URL = "https://..."
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.Navigate URL
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop
HWNDSrc = IE.HWND
SetForegroundWindow HWNDSrc
Set html = IE.document
On Error Resume Next
Set Example = html.getElementsByClassName("Example")(0)
'''''''' Trying to get Result
Set TableRows = Example.Children
For Each TableRow In TableRows
Set RowChildren = TableRow.Children
For Each RowChild In RowChildren
Set TableHeader = RowChild.getElementsByTagName("th")(0)
Set TableData = TableHeader.NextSibling
If TableHeader.innerText = "Header2" Then MsgBox TableData.innerText
Next
Next
IE.Quit
Set IE = Nothing
Application.StatusBar = ""
End Sub