【问题标题】:HtmlAgility Pack is having difficulties to get information from the nested tableHtmlAgilityPack 难以从嵌套表中获取信息
【发布时间】:2012-04-12 15:33:42
【问题描述】:

我正在尝试解析下表,但不幸的是每个节点似乎都相互嵌套。 :( 获取子节点是不可能的,因为它总是给出 count = 1

这真的很有趣,但它正在寻找;例如下一个“tr”作为前一个 tr 的子节点?

你有什么想法吗?

<table width="292px" border="0">
    <tr>
        <td>
        </td>
    </tr>
    <tr>
        <td>
            <table>
                <tr>
                    <td colspan="2" bgcolor="#FBCE9D" align="center" height="40">
                    </td>
                </tr>
                <tr>
                    <td bgcolor="#FFF4D2" height="25" width="60">
                    </td>
                    <td height="25" bgcolor="#e8e8e8">
                    </td>
                </tr>
                <tr>
                    <td bgcolor="#FFF4D2" height="25" width="60">
                    </td>
                    <td height="25" bgcolor="#e8e8e8">
                    </td>
                </tr>
                <tr>
                    <td bgcolor="#FFF4D2" height="25" width="60">
                    </td>
                    <td height="25" bgcolor="#e8e8e8">
                    </td>
                </tr>
                <tr>
                    <td bgcolor="#FFF4D2" height="25" width="60">
                    </td>
                    <td height="25" bgcolor="#e8e8e8">
                    </td> //Here is a missing "</tr>" and I think this one is confusing the agilitypack!
                    <tr>
                        <td bgcolor="#FFF4D2" height="35" colspan="2" align="center">
                        </td>
                    </tr>
            </table>
        </td>
    </tr>
</table>

我的代码是:

var webGet = new HtmlWeb();
var doc = webGet.Load("the url where this table is located");
HtmlNodeCollection tb = doc.DocumentNode.SelectNodes("//table[@width='292px']");
                        var table = tb[0].ChildNodes[1].ChildNodes[0].ChildNodes[0].ChildNodes;
                        for (var na = 0; na < table.Count; na++)
                        { .....do the work.... }

实际上,这段代码之前的工作就像一个魅力,但他们在里面嵌套了另一个表,上面贴着ChildNodes[1],因为不会有ChildNodes[1],它总是ChildNodes[0]

还有一个注意事项; Firebug 将“/html/body/table/tbody/tr[2]/td/table/tbody”显示为嵌套表的 XPath,但您可能注意到“tbody”不熟悉 htmlagility,因为它是由浏览器删除缺少的关闭标签 /tr

【问题讨论】:

  • 你的代码是什么样的?
  • 对不起,我忘了。你可以在上面找到它。

标签: html-parsing html-agility-pack


【解决方案1】:

这真的很有趣,但问题是在 Nuget 上实际可用的 HmtlAgility 包!我将其删除并从网上下载(http://htmlagilitypack.codeplex.com/)。它现在正在工作!

【讨论】:

    【解决方案2】:

    XPATH 将在这里为您提供很多帮助。

    对于内表tr 节点,您可以在下面尝试此操作

    doc.DocumentNode.SelectNodes("//table[@width='292px']/tr/td/table/tr")
    

    如果你想遍历内表中的td节点,那么

    doc.DocumentNode.SelectNodes("//table[@width='292px']/tr/td/table/tr/td")
    

    【讨论】:

    • 不幸的是它没有用。但这一次我也注意到了;这张表上有 64 个解析错误,包括“结束标签 在此处无效”和类似错误……所以我认为他们故意制造这种破碎的结构是为了消除像我们这样的人:(那么,你有更多意见吗?
    • 我也在这个问题上放了一些 cmets。
    • @Gorkem - 不知道为什么它不起作用。您能否详细说明您在寻找什么。您能否提及实际的网址,以便我可以调查它为什么不适合您。 tbody 在代码中不存在,所以你不必担心。我认为错误的HTML 结构可能是一个编程错误,我不认为这是故意的。
    • 我刚刚解析了您的 HTML 片段,我得到了 6 个 tr 节点(包括错误的节点)和 10 个 td 节点。它自动发现缺少 标记并相应地对其进行解析。我这边没有问题。仅供参考,我使用的是 1.4.0.0 版本。
    • 真的很有趣。我也在使用相同的版本。如果您可以尝试使用此网址,我将不胜感激:ismgis.istanbulsaglik.gov.tr/eczane/… 感谢您的大力帮助
    猜你喜欢
    • 2015-05-02
    • 1970-01-01
    • 2021-04-08
    • 2018-06-23
    • 2012-09-11
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多