C#爬蟲,讓你不再覺得神祕
阿新 • • 發佈:2020-12-20
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