【问题标题】:HtmlAgilityPack - How to extract data?HtmlAgilityPack - 如何提取数据?
【发布时间】:2020-05-27 18:35:15
【问题描述】:

如何使用 HtmlAgilityPack 从以下 HTML 中获取 1988:

<span class="year">Year: <strong class="">1988</strong></span>

我试过了:

.Descendants("span").Where(n => n.GetAttributeValue("class", "").Equals("year")).First().Attributes["strong"].DeEntitizeValue;

但返回空值。

【问题讨论】:

    标签: c# html html-agility-pack


    【解决方案1】:

    你想要强元素的InnerText。 在您的示例中,您可以这样做

    SelectSingleNode("//strong").InnerText
    

    根据您的用例适当地修改 strong 选择器。

    Documentation here

    【讨论】:

    • 太好了,谢谢。如果 HTML 文档中有多个 元素,我该怎么办?
    • 在您的示例中,您可以执行//span[@class=year]/strong。否则,这取决于您的特定用例。了解如何使用 XPath。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    相关资源
    最近更新 更多