使用HtmlAgilityPack抓取html标签属性内容

妖狐艹你老母 2022-06-06 01:17 561阅读 0赞

第一步:安装HtmlAgilityPack

20171026124038376

简单抓取一些想要的信息:

  1. static void Main(string[] args)
  2. {
  3. HtmlWeb webClient = new HtmlWeb();
  4. HtmlDocument doc = webClient.Load("http://www.iqiyi.com/v_19rrdze0xk.html#vfrm=24-9-0-1");
  5. #region [获取 head meta property(键) content(值)]
  6. HtmlNodeCollection meat_property_List = doc.DocumentNode.SelectNodes(".//meta[@property]");
  7. if (meat_property_List != null)
  8. {
  9. Console.WriteLine();
  10. Console.WriteLine("[获取 head meta property(键) content(值)]");
  11. foreach (HtmlNode meat_property in meat_property_List)
  12. {
  13. HtmlAttribute property_att = meat_property.Attributes["property"];
  14. HtmlAttribute content_att = meat_property.Attributes["content"];
  15. Console.WriteLine(string.Format("{0}\t:\t{1}", property_att.Value, content_att.Value));
  16. }
  17. }
  18. #endregion
  19. #region [获取 head meta name(键) content(值)]
  20. HtmlNodeCollection meat_name_List = doc.DocumentNode.SelectNodes(".//meta[@name]");
  21. if (meat_name_List != null)
  22. {
  23. Console.WriteLine();
  24. Console.WriteLine("[获取 head meta name(键) content(值)]");
  25. foreach (var meat_name in meat_name_List)
  26. {
  27. HtmlAttribute name_att = meat_name.Attributes["name"];
  28. HtmlAttribute content_att = meat_name.Attributes["content"];
  29. Console.WriteLine(string.Format("{0}\t:\t{1}", name_att.Value, content_att.Value));
  30. }
  31. }
  32. #endregion
  33. #region [获取 title]
  34. Console.WriteLine();
  35. Console.WriteLine("[获取 title]");
  36. var titleObj = doc.DocumentNode.SelectSingleNode("//title");
  37. if (titleObj != null)
  38. {
  39. Console.WriteLine("title:" + titleObj.InnerText);
  40. }
  41. #endregion
  42. #region [获取 首个 img src]
  43. Console.WriteLine();
  44. Console.WriteLine("[获取 首个 img src]");
  45. var imgObj = doc.DocumentNode.SelectSingleNode("//img");
  46. if (imgObj != null)
  47. {
  48. string src = imgObj.Attributes["src"].Value;
  49. Console.WriteLine("img src:" + src);
  50. }
  51. #endregion
  52. Console.ReadKey();
  53. }

20171026124224538

20171026131611056

发表评论

表情:
评论列表 (有 0 条评论,561人围观)

还没有评论,来说两句吧...

相关阅读

    相关 使用 HTML 标签隐藏内容

    在网页开发中,我们经常需要隐藏某些内容并在需要时进行显示。HTML 提供了一些标签和属性,可以帮助我们实现这一目的。下面将详细介绍几种常用的方法。 1. 使用 CSS 的

    相关 HTML5新增标签属性

    关于h5 兜兜转转终于学完CSS开始接h5了!其实h5没有大家想的那么复杂!我们之前学的pc端就是以h5的标准来的 只是那些标签是 html4.01之前就有的!我们后