乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      一個(gè)簡(jiǎn)單的C#爬蟲(chóng)程序

       路人甲Java 2020-05-07

          這篇這篇文章主要是展示了一個(gè)C#語(yǔ)言如何抓取網(wǎng)站中的圖片。實(shí)現(xiàn)原理就是基于http請(qǐng)求。C#給我們提供了HttpWebRequest和WebClient兩個(gè)對(duì)象,方便發(fā)送請(qǐng)求獲取數(shù)據(jù),下面看如何實(shí)

       


      1,HttpGetAction方法。用于發(fā)送請(qǐng)求獲取數(shù)據(jù)后處理字符串得到圖片地址

       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("請(qǐng)求地址錯(cuò)誤");
      18                 Console.ReadKey();
      19                 return;
      20             }
      21             //提取img標(biāo)簽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             //爬取數(shù)量
      26             int i = 0;
      27             WebClient web = new WebClient();
      28             // 取得匹配項(xiàng)列表   
      29             foreach (Match match in matches)
      30             {
      31                 string imgsrc = 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("爬取圖片耗時(shí):" + 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("路徑錯(cuò)誤!");
       7                 Console.ReadKey();
       8                 return;
       9             }
      10             web.DownloadFile(src, path+name+".jpg");
      11             Console.WriteLine("爬取圖片成功:" + name+".jpg");
      12         }

       

      3,控制臺(tái)調(diào)用

      1 static void Main(string[] args)
      2         {
      3             string url= "https://www./";
      4             string path = Path.Combine(@"D:\word 資料\img\冬天\");
      5             HttpHelper.HttpGetAction(url,path,1);
      6             Console.ReadKey();
      7         }

       

      效果圖:

       

       

      一個(gè)簡(jiǎn)單的C#爬蟲(chóng)程序就完成了。如有錯(cuò)誤的地方還望大神指點(diǎn)

       

      原文來(lái)自:一個(gè)簡(jiǎn)單的C#程序-曾亞平個(gè)人博客

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多