我试图通过使用htmlagilitypack解析HTML来从HTML表中获取信息。
以下是HTML的外观:
...
...
...
<tbody>
<tr>
<td class="style_19" style="vertical-align: baseline;">
<div class="style_18">AA00857</div>
</td>
<td class="style_19" style="vertical-align: baseline;">
<div></div>
<div class="style_20">TPRCF</div>
</td>
<td class="style_19" style="vertical-align: baseline;">
<div class="style_21"></div>
</td>
<td class="style_19" style="vertical-align: baseline;">
<div class="style_21">16908/2</div>
</td>
<td class="style_19" style="vertical-align: baseline;">
<div class="style_18"> ETG_C</div>
</td>
</tr>
<tr>
<td class="style_19" style="vertical-align: baseline;">
<div class="style_18">AA01231</div>
</td>
<td class="style_19" style="vertical-align: baseline;">
<div></div>
<div class="style_20">TPRCF</div>
</td>
<td class="style_19" style="vertical-align: baseline;">
<div class="style_21"></div>
</td>
<td class="style_19" style="vertical-align: baseline;">
<div class="style_21">16909/19</div>
</td>
<td class="style_19" style="vertical-align: baseline;">
<div class="style_18"> ETG_C</div>
</td>
</tr>
<tr>
<td class="style_19" style="vertical-align: baseline;">
<div class="style_18">AA01233</div>
</td>
<td class="style_19" style="vertical-align: baseline;">
<div></div>
<div class="style_20">TPRCF</div>
</td>
<td class="style_19" style="vertical-align: baseline;">
<div class="style_21"></div>
</td>
<td class="style_19" style="vertical-align: baseline;">
<div class="style_21">16907/7</div>
</td>
<td class="style_19" style="vertical-align: baseline;">
<div class="style_18"> ETG_C</div>
</td>
</tr>
...
...
我需要从上面的这些值中提取:
AA00857, TPRCF, 16908/2, ETG_C
到目前为止,我所拥有的只是:
HtmlWeb hw = new HtmlWeb();
HtmlAgilityPack.HtmlDocument htmlDoc = hw.Load(@"http://www.some123123site.com/index");
if (htmlDoc.DocumentNode != null)
{
HtmlAgilityPack.HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//tbody");
if (bodyNode != null)
{
// Do something with bodyNode
}
}
请帮忙!
最佳答案
试试这个:
HtmlWeb hw = new HtmlWeb();
HtmlAgilityPack.HtmlDocument htmlDoc = hw.Load(@"http://www.some123123site.com/index");
if (htmlDoc.DocumentNode != null)
{
foreach(HtmlNode text in htmlDoc.DocumentNode.SelectNodes("//tr/td/div/text()"))
{
Console.WriteLine(text.InnerText);
}
}