温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#多线程爬虫如何抓取免费代理IP

发布时间:2021-07-21 09:38:56 来源:亿速云 阅读:137 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关C#多线程爬虫如何抓取免费代理IP,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

这里用到一个HTML解析辅助类:HtmlAgilityPack,如果没有网上找一个增加到库里,这个插件有很多版本,如果你开发环境是使用VS2005就2.0的类库,VS2010就使用4.0,以此类推..........然后直接创建一个控制台应用,将我下面的代码COPY替换就可以运行,下面就来讲讲我两年前做爬虫经历,当时是给一家公司做,也是用的C#,不过当时遇到一个头痛的问题就是抓的图片有病毒,然后系统挂了几次。所以抓网站图片要注意安全,虽然我这里没涉及到图片,但是还是提醒下看文章的朋友。

 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("//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中第一个是能用的代理标题,所以这里从第二行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,表示这个代理速度快。
           if (subSpeed > 90)
            {
              proxy temp = new proxy(ip, port, subSpeed);
              
              masterPorxyList.Add(temp);
              Console.WriteLine("当前是第:" + masterPorxyList.Count.ToString() + "个代理IP");
            }
          
           }


        }
 
    }

    //抓网页方法
    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)
     {
       //多线程同时抓15页
       for (int i = 1; i <= 15; i++)
       {
        
         
         ThreadPool.QueueUserWorkItem(getProxyList, i);
       }
       Console.Read();
     }

  }

关于“C#多线程爬虫如何抓取免费代理IP”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI