HTML Agility Pack - 从 TD 和 TR 标签中检索多个值



我是HTML敏捷包的新手,我需要在网页上获取一些值,但无法找到方法。希望你能帮助我。

此致敬意。

示例 HTML 部分:

...
<div id="page-bgtop">
<div id="page-bgbtm">
<div id="content">
<div class="post">
<div class="entry">
<center>
<div/>
<div>
<table class="CSSTableGenerator" cellspacing="0" cellpadding="4" border="0" id="GridView2" style="width: 98%; border-collapse: collapse;">
<tbody>
<tr style="font-weight: bold;">
<tr style="background-color: rgb(239, 243, 251);">
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">506</a>
</td>
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">29/06/2017</a>
</td>
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">4</a>
</td>
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">20</a>
</td>
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">21</a>
</td>
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">32</a>
</td>
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">43</a>
</td>
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">47</a>
</td>
</tr>
<tr style="background-color: white;">
<td>
...

我想得到的值:506 29/06/2017 4 20 21 32 43 47

PS:这只是一个示例html部分,有很多行值。

使用 XPath 后代或自身轴 (//(,特别是//tr选择所有表格行,然后检查每个单元格中超链接的行:

var document = new HtmlDocument();
document.LoadHtml(html);
//               ^^^^^^ your HTML snippet above
var rows = document.DocumentNode.SelectNodes("//tr");
if (rows != null && rows.Count > 0)
{
foreach (var row in rows)
{
var links = row.SelectNodes("./td/a");
if (links != null) Console.WriteLine(string.Join(" ", links.Select(x => x.InnerText)));
}
}

输出:

506 2017-06-29 4 20 21 32 43 47

最新更新