【问题标题】:HTML Parser with AngleSharp - Text in IElement带有 AngleSharp 的 HTML 解析器 - IElement 中的文本
【发布时间】:2017-01-26 03:40:41
【问题描述】:

我正在用 AngleSharp 编写一个 HTML 解析器,它应该像这样输入 HTML:

<p>
Paragraph Text
<a href="https://www.example com" class="external text" target="_new" rel="nofollow">Link Text</a>
Paragraph Text 2
</p>

并像这样输出:

<p>
Paragraph Text
<a href="https://www.example com">Link Text</a>
Paragraph Text 2
</p>

我写了这个递归函数来遍历整个文档:

using AngleSharp.Dom;
using AngleSharp.Dom.Html;
using AngleSharp.Extensions;
using AngleSharp.Parser.Html;

private void processHTMLNode(IElement node, IElement targetNode)
{
    switch (node.NodeName.ToLower())
    {
    //...
    case "a":
        if(node.HasAttribute("href") && node.GetAttribute("href").StartsWith("#"))
        {
            break;
        }
        var aNew = outputDocument.CreateElement("a");
        aNew.SetAttribute("href", node.GetAttribute("href"));
        aNew.TextContent = node.TextContent;
        targetNode.AppendChild(aNew);
        break;
    case "p":
        var pNew = outputDocument.CreateElement<IHtmlParagraphElement>();
        foreach (var childNode in node.Children)
        {
            processHTMLNode(childNode, pNew);
        }
        //TODO fix this
        pNew.TextContent = node.TextContent;
        targetNode.AppendChild(pNew);
        break;
    }
    //...
}

问题在于,设置TextContent 属性会覆盖a-元素,它们是p-Node 的子元素。顺序(文本 -> 链接 -> 文本)也丢失了。

我该如何正确实现这个?

【问题讨论】:

    标签: c# html parsing anglesharp


    【解决方案1】:

    好的,所以我设法使用以下代码解决了我的问题:

    using AngleSharp.Dom;
    using AngleSharp.Dom.Html;
    using AngleSharp.Extensions;
    using AngleSharp.Parser.Html;
    
    private void processHTMLNode(INode node, IElement targetElement)
    {
        IElement elementNode;
        IText textNode;
    
        if ((elementNode = node as IElement) != null)
        {
            switch (node.NodeName.ToLower())
            {
                //...
                case "a":
                    if(node.HasAttribute("href") && node.GetAttribute("href").StartsWith("#"))
                    {
                        break;
                    }
                    var aNew = outputDocument.CreateElement("a");
                    aNew.SetAttribute("href", node.GetAttribute("href"));
                    foreach (var childNode in elementNode.ChildNodes)
                    {
                        processHTMLNode(childNode, aNew);
                    }
                    targetElement.AppendChild(aNew);
                    break;
                case "p":
                    var pNew = outputDocument.CreateElement("p");
                    foreach (var childNode in node.Children)
                    {
                        processHTMLNode(childNode, pNew);
                    }
                    targetElement.AppendChild(pNew);
                    break;
                //...
            }
    
        }
        else if ((textNode = node as IText) != null)
        {
            var newTextNode = outputDocument.CreateTextNode(textNode.Text);
            targetElement.AppendChild(newTextNode);
        }
    }
    

    AngleSharp 文档中的这张图片对我帮助很大: AngleSharp DOM

    【讨论】:

      猜你喜欢
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 2017-10-16
      • 2020-03-31
      • 2012-05-25
      相关资源
      最近更新 更多