static void Main(string[] args) { var page = 1;//抓取的頁數 //抓取網頁資源 for (int i = 1; i <= page; i++) { string str = GetHtmlStr($"https://fabiaoqing.com/biaoqing/lists/page/{i}.html", "UTF8"); //匹配圖片的正則表達式 string regstr = "http://wx[1-4].sinaimg.cn/bmiddle/.+?.[jg][pi][fg]"; foreach (Match match in Regex.Matches(str, regstr)) //使用正則表達式解析網頁文本,獲得圖片地址 { //下載圖片 SaveAsWebImg(match.Value); } } Console.ReadKey(); Console.WriteLine("已執行結束,按任意鍵退出!"); }
/// <summary> /// 獲取網頁的HTML碼 /// </summary> /// <param name="url">鏈接地址</param> /// <param name="encoding">編碼類型</param> /// <returns></returns> public static string GetHtmlStr(string url, string encoding) { string htmlStr = ""; if (!String.IsNullOrEmpty(url)) { WebRequest request = WebRequest.Create(url); //實例化WebRequest對象 WebResponse response = request.GetResponse(); //創建WebResponse對象 Stream datastream = response.GetResponseStream(); //創建流對象 Encoding ec = Encoding.Default; if (encoding == "UTF8") { ec = Encoding.UTF8; } else if (encoding == "Default") { ec = Encoding.Default; } StreamReader reader = new StreamReader(datastream, ec); htmlStr = reader.ReadToEnd(); //讀取數據 reader.Close(); datastream.Close(); response.Close(); } return htmlStr; }
/// <summary> /// 下載網站圖片 /// </summary> /// <param name="picUrl"></param> /// <returns></returns> public static string SaveAsWebImg(string picUrl) { string result = ""; //設置保存目錄 string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"/File/"; //不存在目錄則創建 if (!Directory.Exists(path)) { //創建目錄 Directory.CreateDirectory(path); } try { //判斷圖片是否為空或者null if (!String.IsNullOrEmpty(picUrl)) { //偽隨機數生成器 Random rd = new Random(); //獲取當前日期時間 DateTime nowTime = DateTime.Now; //獲取URL擴展名 var Extension = Path.GetExtension(picUrl); //自定義文件名 string fileName = nowTime.Month.ToString() + nowTime.Day.ToString() + nowTime.Hour.ToString() + nowTime.Minute.ToString() + nowTime.Second.ToString() + rd.Next(1000, 1000000) + Extension; WebClient webClient = new WebClient(); //下載url鏈接文件,並指定到本地的文件夾路徑和文件名稱 webClient.DownloadFile(picUrl, path + fileName); //返回結果 result = fileName; } }catch(Exception ex) { Console.WriteLine(ex.Message); } return result; }