HTML Agility Pack - 从 TD 和 TR 标签中检索多个值
HTML Agility Pack - retrieving multiple values from TD & TR tags
我是 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 descendant-or-self axis (//
),特别是 //tr
到 select 所有 table 行,然后检查行每个单元格中的超链接:
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 29/06/2017 4 20 21 32 43 47
我是 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 descendant-or-self axis (//
),特别是 //tr
到 select 所有 table 行,然后检查行每个单元格中的超链接:
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 29/06/2017 4 20 21 32 43 47