【问题标题】:get what is between html tags获取 html 标签之间的内容
【发布时间】:2015-03-31 14:19:53
【问题描述】:

我想使用 HtmlAgilityPack 获取 html 标签之间的文本,我知道它的基本类型,但我需要检查并完全理解该库的工作原理。

这里是简单的代码:

{
class Program
{
    static void Main(string[] args)
    {
        HtmlWeb htmlWeb = new HtmlWeb();
        HtmlDocument htmlDocument = htmlWeb.Load("http://192.168.1.177/");
        Console.WriteLine();// write the html 
        Console.ReadLine();
    }
}

}

这是来自远程传感器的我的页面内容:

我希望我的输出是:

模拟输入 0 是 213
模拟输入 1 为 201
模拟输入 2 为 206
模拟输入 3 为 214
模拟输入 4 为 223
模拟输入 5 为 240

没有 html 和 /html 标签

【问题讨论】:

    标签: c# html html-agility-pack


    【解决方案1】:
    htmlDocument.DocumentNode.InnerText
    
    • DocumentNode代表HTML文档的根节点
    • InnerText 包含当前HtmlNode 中的所有文本节点,并连接在一起(注意DocumentNode 属性的类型为HtmlNode)。

    【讨论】:

    • 感谢@har07 我不能摆脱 吗?使用 HtmlAgilityPack 或者我应该只使用常规 exp 或任何其他方法?
    • @SabirMoglad 一种可能的方式:doc.DocumentNode.SelectSingleNode("html").InnerText.Trim()
    • Trim() 只是为了删除“不必要的”前导和尾随空格
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2015-07-07
    • 2015-02-08
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    相关资源
    最近更新 更多