我只是再次使用HTMLAgilityPack而且我真的没有想法为什么我的代码以这种方式执行。所以我有这个HTML代码。这只是一个实验HTML代码,可以让您更轻松地进行实验。
<html>
<tbody class="person">
<div class="name">Chris</div>
<td class="num">3,200</td>
<td class="num">62,000</td>
<td class="num">59,700</td>
<td class="num">1,500</td>
</tbody>
<tbody class="person">
<div class="name">John</div>
<td class="num">23,200</td>
<td class="num">900</td>
<td class="num">60,100</td>
<td class="num">1,300</td>
</tbody>
<tbody class="person">
<div class="name">James</div>
<td class="num">900</td>
<td class="num">60</td>
<td class="num">61,400</td>
<td class="num">610</td>
</tbody>
</html>
到目前为止,我目前的代码是......
Dim web As New HtmlAgilityPack.HtmlWeb
Dim doc As HtmlAgilityPack.HtmlDocument = web.Load("C:/sample.html")
For Each tbody As HtmlNode In doc.DocumentNode.SelectNodes("//tbody[contains(@class, 'person')]")
Dim numberNode = tbody.SelectSingleNode("//tbody[contains(@class, 'person')]")
Dim valueNode = numberNode.SelectNodes(".//td[contains(@class, 'num')]")
Dim numbers As String()
numbers = (From n In valueNode Select n.InnerText).ToArray()
Dim nameNode As HtmlNode = tbody.SelectSingleNode(".//div[contains(@class, 'name')]")
If nameNode IsNot Nothing Then
MsgBox(numbers(0) & " " & numbers(1) & " " & numbers(2) & " " & numbers(3) & " - " & nameNode.InnerText, MsgBoxStyle.OkOnly, "System Message")
End If
Next
我真的很困惑,为什么它只返回第一个数字值。数值不与<div class="name">
对应。
根据我的理解,对于每个循环,它会获得每个<tbody class="person">
下的相应数据。如何获取与<div class="name">
对应的下一个数字值。非常感谢你。
答案 0 :(得分:2)
这是错误:
'select the first <tbody class="person"> element'
Dim numberNode = tbody.SelectSingleNode("//tbody[contains(@class, 'person')]")
'then get all <td class='num'> within above <tbody>'
Dim valueNode = numberNode.SelectNodes(".//td[contains(@class, 'num')]")
尝试将其更改为:
'get all <td class='num'> within <tbody> of current iteration'
Dim valueNode = tbody.SelectNodes(".//td[contains(@class, 'num')]")