HtmlAgilityPackHelper.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using HtmlAgilityPack;
  2. namespace GxPress.Common.Tools
  3. {
  4. public static class HtmlAgilityPackHelper
  5. {
  6. /// <summary>
  7. /// 完整
  8. /// </summary>
  9. /// <param name="html"></param>
  10. /// <returns></returns>
  11. public static string GetHmtl(string html)
  12. {
  13. var document = new HtmlDocument();
  14. document.LoadHtml(html);
  15. var node = document.DocumentNode.SelectNodes("//body/p");
  16. var tittle = document.DocumentNode.SelectNodes("//title");
  17. var pContent = $"<h2>{tittle[0].InnerText}</h2>";
  18. foreach (var item in node)
  19. {
  20. if (string.IsNullOrWhiteSpace(item.InnerHtml))
  21. continue;
  22. pContent += item.OuterHtml;
  23. }
  24. return pContent;
  25. }
  26. /// <summary>
  27. /// 一部分
  28. /// </summary>
  29. /// <param name="html"></param>
  30. /// <returns></returns>
  31. public static string GetHmtlContent(string html)
  32. {
  33. var pContent = string.Empty;
  34. var document = new HtmlDocument();
  35. document.LoadHtml(html);
  36. var node = document.DocumentNode.SelectNodes("//body/p");
  37. var tittle = document.DocumentNode.SelectNodes("//title");
  38. pContent = $"<h2>{tittle[0].InnerText}</h2>";
  39. foreach (var item in node)
  40. {
  41. if (pContent.Length > 100)
  42. break;
  43. if (string.IsNullOrWhiteSpace(item.InnerHtml))
  44. continue;
  45. //pContent += item.InnerHtml.ToString()
  46. pContent += item.OuterHtml;
  47. }
  48. return pContent;
  49. }
  50. }
  51. }