HTML Agility Pack - 从TD& amp;中检索多个值TR标签

时间:2017-07-03 19:19:13

标签: html html-parsing html-agility-pack

我是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部分,有很多行值。

1 个答案:

答案 0 :(得分:1)

使用XPath descendant-or-self axis//),特别是//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 29/06/2017 4 20 21 32 43 47