【问题标题】:HTML Agility Pack - retrieving multiple values from TD & TR tagsHTML Agility Pack - 从 TD 和 TR 标签中检索多个值
【发布时间】:2017-07-03 19:19:12
【问题描述】:

我是 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 2017 年 6 月 29 日 4 20 21 32 43 47

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

【问题讨论】:

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


    【解决方案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

    【讨论】:

    • 还有一件事,如何将这些值插入到具有完全相同列的表中。 (drawNo,drawDate,ball1,ball2,ball3,ball4,ball5,ball6)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多