繁体   English   中英

使用 c# 从 html 提取格式化文本和 href 链接

[英]extract formatted text and also href link from html using c#

我正在尝试提取文本以及href中的链接。 <html><body><p>foo <a href='http://www.example.com'>bar</a><br> baz</p></body></html>我在找对于 output as, foo http://www.example.com bar baz br 标签应该被考虑以获得正确的格式化句子。

这里是 go:

using System;
using HtmlAgilityPack;
                    
public class Program
{
    public static void Main()
    {
        var html =
        @"<html><body><p>foo <a href='http://www.example.com'>bar</a><br> baz</p></body></html> ";
        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);
        var htmlAnchor = htmlDoc.DocumentNode.SelectSingleNode("//a");
        var htmlBr = htmlDoc.DocumentNode.SelectSingleNode("//p");
        string hrefValue = htmlAnchor.Attributes["href"].Value;
        Console.WriteLine(htmlBr.InnerText + " " + hrefValue);
    }
}

Output:

富吧 baz http://www.example.com

工作示例: https://dotnetfiddle.net/BBYAF9

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM