12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- using HtmlAgilityPack;
- namespace GxPress.Common.Tools
- {
- public static class HtmlAgilityPackHelper
- {
- /// <summary>
- /// 完整
- /// </summary>
- /// <param name="html"></param>
- /// <returns></returns>
- public static string GetHmtl(string html)
- {
- var document = new HtmlDocument();
- document.LoadHtml(html);
- var node = document.DocumentNode.SelectNodes("//body/p");
- var tittle = document.DocumentNode.SelectNodes("//title");
- var pContent = $"<h2>{tittle[0].InnerText}</h2>";
- foreach (var item in node)
- {
- if (string.IsNullOrWhiteSpace(item.InnerHtml))
- continue;
- pContent += item.OuterHtml;
- }
- return pContent;
- }
- /// <summary>
- /// 一部分
- /// </summary>
- /// <param name="html"></param>
- /// <returns></returns>
- public static string GetHmtlContent(string html)
- {
- var pContent = string.Empty;
- var document = new HtmlDocument();
- document.LoadHtml(html);
- var node = document.DocumentNode.SelectNodes("//body/p");
- var tittle = document.DocumentNode.SelectNodes("//title");
- pContent = $"<h2>{tittle[0].InnerText}</h2>";
- foreach (var item in node)
- {
- if (pContent.Length > 100)
- break;
- if (string.IsNullOrWhiteSpace(item.InnerHtml))
- continue;
- //pContent += item.InnerHtml.ToString()
- pContent += item.OuterHtml;
- }
- return pContent;
- }
- }
- }
|