【问题标题】:XPath All A within a TD which also contains an H3 with the innertext of "Directs"TD 中的 XPath All A 还包含带有“Directs”内部文本的 H3
【发布时间】:2012-02-15 19:44:15
【问题描述】:

我正在抓取一个网站。有一个 TD,其中第一个子节点是 H3,内部文本为“Directs”。在 TD 中,其他孩子(除了 H3)是链接。我知道 XPath 完全有能力从 TD 中返回 A 标记子项,该 TD 还包含带有“Directs”内部文本的 H3,我似乎无法正确处理。我想出的丑陋解决方法如下,但我想学习最好的 XPath 方法:

For Each thisH3 As HtmlNode In Doc.SelectNodes("//h3")
    If thisH3.InnerText = "Directs" Then
        For Each nChild As HtmlNode In thisH3.ParentNode.ChildNodes
            If nChild.Name = "a" Then
                Debug.Print(nChild.InnerText)
            End If
        Next
    End If
Next

【问题讨论】:

    标签: .net xml dom xpath html-agility-pack


    【解决方案1】:

    使用此 XPath 检索 td 中的所有 a,其中 h3 的值为 Directs

    //td[h3 = 'Directs']/a
    

    【讨论】:

    • 如果您的 <a> 可能包含在 TD 的其他标记中,您可能需要在其中添加另一个斜杠://td[h3 = 'Directs']//a
    • As 是TD 的直接子代,但这是很好的信息,感谢@Phrogz
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多