簡體   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