【问题标题】:HtmlAgilityPack clean inner text from htmlHtmlAgilityPack 从 html 清除内部文本
【发布时间】:2011-12-08 02:58:54
【问题描述】:

我有这个 html。我正在尝试获取其中没有任何标签的 InnerText,

<h1>my h1 content</h1>
<div class="thisclass">
<p> some text</p>
<p> some text</p>
    <div style="some_style">
    some text
        <script type="text/javascript">
        <!-- some script -->
        </script>
    <script type='text/javascript' src='some_script.js'></script>
    </div>
<p> some text<em>some text</em>some text.<em> <br /><br /></em><strong><em>some text</em></strong></p>
    <p> </p>
   </div>

我试图做的是获取用户从类thisclass中看到的文本。 我想去掉任何脚本标签和所有标签,只得到纯文本。

这是正在使用的:

 Dim Tags As HtmlNodeCollection = root.SelectNodes("//div[@class='thisclass'] | //h1")

有人有什么想法吗?

谢谢。

【问题讨论】:

    标签: vb.net dom html-parsing html-agility-pack strip-tags


    【解决方案1】:

    试试这个(警告 c# 代码):

    foreach(var script in root.SelectNodes("//script"))
    {
        script.ParentNode.RemoveChild(script);
    }
    
    Console.WriteLine(root.InnerText);
    

    这给了我以下输出:

    my h1 content some text some textsome text    some textsome textsome text. some text
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-02-16
      • 1970-01-01
      • 2014-04-30
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多