這篇這篇文章主要是展示了一個C#語言如何抓取網站中的圖片,實作原理就是基于http請求,C#給我們提供了HttpWebRequest和WebClient兩個物件,方便發送請求獲取資料,下面看如何實
1,HttpGetAction方法,用于發送請求獲取資料后處理字串得到圖片地址
1 public static void HttpGetAction(string url,string path,int name) 2 { 3 Stopwatch sw = new Stopwatch(); 4 sw.Start(); 5 Console.WriteLine("抓取地址:" + url); 6 string result = string.Empty; 7 HttpWebRequest webRequest = WebRequest.CreateHttp(url); 8 webRequest.Method = "GET"; 9 var response= webRequest.GetResponse(); 10 using (StreamReader reader = new StreamReader((response as HttpWebResponse).GetResponseStream(), Encoding.UTF8)) 11 { 12 result = reader.ReadToEnd(); 13 reader.Close(); 14 } 15 if (string.IsNullOrEmpty(result)) 16 { 17 Console.WriteLine("請求地址錯誤"); 18 Console.ReadKey(); 19 return; 20 } 21 //提取img標簽src地址 22 Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase); 23 // 搜索匹配的字串 24 MatchCollection matches = regImg.Matches(result); 25 //爬取數量 26 int i = 0; 27 WebClient web = new WebClient(); 28 // 取得匹配項串列 29 foreach (Match match in matches) 30 { 31 string imgsrc = https://www.cnblogs.com/zyp520/p/match.Groups["imgUrl"].Value; 32 if (imgsrc.Contains("http") && !imgsrc.Contains(".svg")) 33 { 34 i++; 35 HttpGetImg(web,imgsrc, path,name); 36 name++;//圖片名 37 } 38 } 39 sw.Stop(); 40 Console.WriteLine("爬取完成!總共爬取了" + i + "張圖片!"); 41 Console.WriteLine("爬取圖片耗時:" + sw.ElapsedMilliseconds / 1000 + "秒"); 42 }
2,HttpGetImg方法,下載圖片到指定目錄
1 public static void HttpGetImg(WebClient web, string src,string path,int name) 2 { 3 Console.WriteLine("爬取圖片:" + src); 4 if (!Directory.Exists(path)) 5 { 6 Console.WriteLine("路徑錯誤!"); 7 Console.ReadKey(); 8 return; 9 } 10 web.DownloadFile(src, path+name+".jpg"); 11 Console.WriteLine("爬取圖片成功:" + name+".jpg"); 12 }
3,控制臺呼叫
1 static void Main(string[] args) 2 { 3 string url= "https://www.xxxxxx.com/"; 4 string path = Path.Combine(@"D:\word 資料\img\冬天\"); 5 HttpHelper.HttpGetAction(url,path,1); 6 Console.ReadKey(); 7 }
效果圖:

一個簡單的C#爬蟲程式就完成了,如有錯誤的地方還望大神指點
原文來自:一個簡單的C#程式-曾亞平個人博客
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/101904.html
標籤:C#
