【问题标题】:htmlagilitypack - remove script and style?htmlagilitypack - 删除脚本和样式?
【发布时间】:2012-11-06 15:31:45
【问题描述】:

我用下面的方法来提取html表单中的文本:

    public string getAllText(string _html)
    {
        string _allText = "";
        try
        {
            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(_html);


            var root = document.DocumentNode;
            var sb = new StringBuilder();
            foreach (var node in root.DescendantNodesAndSelf())
            {
                if (!node.HasChildNodes)
                {
                    string text = node.InnerText;
                    if (!string.IsNullOrEmpty(text))
                        sb.AppendLine(text.Trim());
                }
            }

            _allText = sb.ToString();

        }
        catch (Exception)
        {
        }

        _allText = System.Web.HttpUtility.HtmlDecode(_allText);

        return _allText;
    }

问题是我也得到了脚本和样式标签。

我怎样才能排除它们?

【问题讨论】:

  • 内联样式,即

    怎么样?我在 OuterHtml 中看到它,但也想去掉所有内联样式。

  • if (childNode.Attributes.Contains("style")) { childNode.Attributes.Remove("style"); } if (childNode.Attributes.Contains("class")) { childNode.Attributes.Remove("class"); }

标签: c# html-agility-pack


【解决方案1】:
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

doc.DocumentNode.Descendants()
                .Where(n => n.Name == "script" || n.Name == "style")
                .ToList()
                .ForEach(n => n.Remove());

【讨论】:

  • 我该如何解决这个问题?
  • @Jacqueline 当你运行上面的代码时。所有scriptstyle 标签将从doc 中删除
  • 啊,我明白了,是否可以扩展支持诸如 之类的 cmets?
  • @Jacqueline .Where(n => n.Name == "script" || n.Name == "style" || n.Name=="#comment")
  • Name 属性是否需要区分大小写?我认为攻击者可能使用了<SCRIPT>
【解决方案2】:

您可以使用HtmlDocument 类:

HtmlDocument doc = new HtmlDocument();

doc.LoadHtml(input);

doc.DocumentNode.SelectNodes("//style|//script").ToList().ForEach(n => n.Remove());

【讨论】:

  • 不应该是doc.DocumentNode.SelectNodes("//style|//script").ToList().ForEach(n => n.Remove());吗?
  • @Rubanov 是的,应该是,我有一个扩展方法,所以我的代码中不需要 .ToList。答案已更新,谢谢。
【解决方案3】:

一些很好的答案,System.Linq 很方便!

对于非基于 Linq 的方法:

private HtmlAgilityPack.HtmlDocument RemoveScripts(HtmlAgilityPack.HtmlDocument webDocument)
{

// Get all Nodes: script
HtmlAgilityPack.HtmlNodeCollection Nodes = webDocument.DocumentNode.SelectNodes("//script");

// Make sure not Null:
if (Nodes == null)
    return webDocument;

// Remove all Nodes:
foreach (HtmlNode node in Nodes)
    node.Remove();

return webDocument;

}

【讨论】:

    【解决方案4】:
    public static string StripStyles(this string html)
    {
        var document = new HtmlDocument();
    
        document.LoadHtml(html);
    
        foreach (var node in document.DocumentNode.DescendantsAndSelf())
        {
            var toRemove = node.Attributes.Where(x => x.Name == "style" || x.Name == "script")
                .ToList();
            foreach (var attribute in toRemove)
            {
                attribute.Remove();
            }
        }
    
        return document.DocumentNode.OuterHtml;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 2014-12-20
      • 2015-08-14
      • 2011-07-10
      • 1970-01-01
      相关资源
      最近更新 更多