C#:HtmlAgilityPack提取内部文本 [英] C#: HtmlAgilityPack extract inner text

查看:146
本文介绍了C#:HtmlAgilityPack提取内部文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HtmlAgilityPack。有没有一行代码可以获取html的所有内部文本,例如,删除所有html标签和脚本?

I am using HtmlAgilityPack. Is there a one line code that I can get all inner text of html, e.g., remove all html tags and scripts?

推荐答案

像这样:

document.DocumentNode.InnerText

请注意,这将返回< script> 标记的文本内容。

Note that this will return the text content of <script> tags.

要解决此问题,您可以删除所有< script> 标记,如下所示:

To fix that, you can remove all of the <script> tags, like this:

foreach(var script in doc.DocumentNode.Descendants("script").ToArray())
    script.Remove();
foreach(var style in doc.DocumentNode.Descendants("style").ToArray())
    style.Remove();

这篇关于C#:HtmlAgilityPack提取内部文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆