欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#多線程爬蟲抓取免費(fèi)代理IP的示例代碼

 更新時(shí)間:2017年08月28日 09:28:45   作者:L-H  
本篇文章主要介紹了C#多線程爬蟲抓取免費(fèi)代理IP的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

這里用到一個(gè)HTML解析輔助類:HtmlAgilityPack,如果沒有網(wǎng)上找一個(gè)增加到庫里,這個(gè)插件有很多版本,如果你開發(fā)環(huán)境是使用VS2005就2.0的類庫,VS2010就使用4.0,以此類推..........然后直接創(chuàng)建一個(gè)控制臺(tái)應(yīng)用,將我下面的代碼COPY替換就可以運(yùn)行,下面就來講講我兩年前做爬蟲經(jīng)歷,當(dāng)時(shí)是給一家公司做,也是用的C#,不過當(dāng)時(shí)遇到一個(gè)頭痛的問題就是抓的圖片有病毒,然后系統(tǒng)掛了幾次。所以抓網(wǎng)站圖片要注意安全,雖然我這里沒涉及到圖片,但是還是提醒下看文章的朋友。

 class Program
  {
    //存放所有抓取的代理
    public static List<proxy> masterPorxyList = new List<proxy>();
    //代理IP類
    public class proxy
    {
      public string ip;

      public string port;
      public int speed;

      public proxy(string pip,string pport,int pspeed)
      
      {
        this.ip = pip;
        this.port = pport;
        this.speed = pspeed;
       }


    }
    //抓去處理方法
    static void getProxyList(object pageIndex)
    {

      string urlCombin = "http://www.xicidaili.com/wt/" + pageIndex.ToString();
      string catchHtml = catchProxIpMethord(urlCombin, "UTF8");
      

      HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
      doc.LoadHtml(catchHtml);


      HtmlNode table = doc.DocumentNode.SelectSingleNode("http://div[@id='wrapper']//div[@id='body']/table[1]");

      HtmlNodeCollection collectiontrs = table.SelectNodes("./tr");  

 
      
        for (int i = 0; i < collectiontrs.Count; i++)
        {
          HtmlAgilityPack.HtmlNode itemtr = collectiontrs[i];


          HtmlNodeCollection collectiontds = itemtr.ChildNodes;
          //table中第一個(gè)是能用的代理標(biāo)題,所以這里從第二行TR開始取值
          if (i>0)
          {
            HtmlNode itemtdip = (HtmlNode)collectiontds[3];

            HtmlNode itemtdport = (HtmlNode)collectiontds[5];

            HtmlNode itemtdspeed = (HtmlNode)collectiontds[13];
 
            string ip = itemtdip.InnerText.Trim();
            string port = itemtdport.InnerText.Trim();


            string speed = itemtdspeed.InnerHtml;
            int beginIndex = speed.IndexOf(":", 0, speed.Length);
            int endIndex = speed.IndexOf("%", 0, speed.Length);

            int subSpeed = int.Parse(speed.Substring(beginIndex + 1, endIndex - beginIndex - 1));
            //如果速度展示條的值大于90,表示這個(gè)代理速度快。
           if (subSpeed > 90)
            {
              proxy temp = new proxy(ip, port, subSpeed);
              
              masterPorxyList.Add(temp);
              Console.WriteLine("當(dāng)前是第:" + masterPorxyList.Count.ToString() + "個(gè)代理IP");
            }
          
           }


        }
 
    }

    //抓網(wǎng)頁方法
    static string catchProxIpMethord(string url,string encoding )
    {

      string htmlStr = "";
      try
      {
        if (!String.IsNullOrEmpty(url))
        {
          WebRequest request = WebRequest.Create(url);  
          WebResponse response = request.GetResponse();      
          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();
        }
      }
      catch { }
      return htmlStr;
    }


   static void Main(string[] args)
     {
       //多線程同時(shí)抓15頁
       for (int i = 1; i <= 15; i++)
       {
        
         
         ThreadPool.QueueUserWorkItem(getProxyList, i);
       }
       Console.Read();
     }

  }

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論