2020-12-20

C#爬虫,让你不再觉得神秘

1、使用第三方类库 HtmlAgilityPack

官方网址:https://html-agility-pack.net/?z=codeplex、

// From File 从文件获取html信息var doc = new HtmlDocument();doc.Load(filePath);// From String 从字符串获取html信息var doc = new HtmlDocument();doc.LoadHtml(html);// From Web 从网址获取html信息var url = "http://html-agility-pack.net/";var web = new HtmlWeb();var doc = web.Load(url);

1.1、这里介绍一下最后一种用法

var web = new HtmlWeb();var doc = web.Load(url);

web 中我们还可以设置cookie、headers等信息,来处理一些特定的网站需求,比如需要登陆等。

1.2 用法解释

网页在你查看网页源代码之后只是一段字符串,而爬虫所做的就是在这堆字符串中,查询到我们想要的信息,挑选出来。
以往的筛选方法:正则 (太麻烦了,写起来有些头疼)
HtmlAgilityPack 支持通过XPath来解析我们需要的信息。

1.2.1 在哪里找XPath?

网页右键检查

通过XPath就可以准确获取你想要元素的全部信息。

1.2.2 获取选中Html元素的信息?

获取选中元素

var web = new HtmlWeb();var doc = web.Load(url);var htmlnode = doc?.DocumentNode?.SelectSingleNode("/html/body/header")

获取元素信息

htmlnode.InnerText;htmlnode.InnerHtml;//根据属性取值htmlnode?.GetAttributeValue("src", "未找到")

2、自己封装的类库

 /// <summary> /// 下载HTML帮助类 /// </summary> public static class LoadHtmlHelper {  /// <summary>  /// 从Url地址下载页面  /// </summary>  /// <param name="url"></param>  /// <returns></returns>  public async static ValueTask<HtmlDocument> LoadHtmlFromUrlAsync(string url)  {   HtmlWeb web = new HtmlWeb();    return await     web?.LoadFromWebAsync(url);  }  /// <summary>  /// 获取单个节点扩展方法  /// </summary>  /// <param name="htmlDocument">文档对象</param>  /// <param name="xPath">xPath路径</param>  /// <returns></returns>  public static HtmlNode GetSingleNode(this HtmlDocument htmlDocument, string xPath)  {   return htmlDocument?.DocumentNode?.SelectSingleNode(xPath);  }  /// <summary>  /// 获取多个节点扩展方法  /// </summary>  /// <param name="htmlDocument">文档对象</param>  /// <param name="xPath">xPath路径</param>  /// <returns></returns>  public static HtmlNodeCollection GetNodes(this HtmlDocument htmlDocument, string xPath)  {   return htmlDocument?.DocumentNode?.SelectNodes(xPath);  }    /// <summary>  /// 获取多个节点扩展方法  /// </summary>  /// <param name="htmlDocument">文档对象</param>  /// <param name="xPath">xPath路径</param>  /// <returns></returns>  public static HtmlNodeCollection GetNodes(this HtmlNode htmlNode, string xPath)  {   return htmlNode?.SelectNodes(xPath);  }  /// <summary>  /// 获取单个节点扩展方法  /// </summary>  /// <param name="htmlDocument">文档对象</param>  /// <param name="xPath">xPath路径</param>  /// <returns></returns>  public static HtmlNode GetSingleNode(this HtmlNode htmlNode, string xPath)  {   return htmlNode?.SelectSingleNode(xPath);  }  /// <summary>  /// 下载图片  /// </summary>  /// <param name="url">地址</param>  /// <param name="filpath">文件路径</param>  /// <returns></returns>  public async static ValueTask<bool> DownloadImg(string url ,string filpath)  {   HttpClient httpClient = new HttpClient();   try   {    var bytes = await httpClient.GetByteArrayAsync(url);    using (FileStream fs = File.Create(filpath))    {     fs.Write(bytes, 0, bytes.Length);    }    return File.Exists(filpath);   }   catch (Exception ex)   {        throw new Exception("下载图片异常", ex);   }     } }

3、自己写的爬虫案例,爬取的网站https://www.meitu131.com/

数据存储层没有实现,懒得写了,靠你们喽,我是数据暂时存在了文件中
GitHub地址:https://github.com/ZhangQueque/quewaner.Crawler.git









原文转载:http://www.shaoqun.com/a/501444.html

跨境电商:https://www.ikjzd.com/

好东东网:https://www.ikjzd.com/w/1238

auditor:https://www.ikjzd.com/w/2437


1、使用第三方类库HtmlAgilityPack官方网址:https://html-agility-pack.net/?z=codeplex、//FromFile从文件获取html信息vardoc=newHtmlDocument();doc.Load(filePath);//FromString从字符串获取html信息vardoc=newHtmlDocument();doc.LoadHtml(ht
bol:bol
woot:woot
五一适合去哪里旅游?:五一适合去哪里旅游?
佛山到正佳极地海洋世界自驾怎么走?佛山到广州正佳海洋馆开车:佛山到正佳极地海洋世界自驾怎么走?佛山到广州正佳海洋馆开车
2019年尾了,这些"作死"的操作千万要避免!:2019年尾了,这些"作死"的操作千万要避免!

No comments:

Post a Comment