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();
         }

    }
时间: 2024-08-07 21:18:22

C#多线程爬虫抓取免费代理IP的相关文章

scrapy抓取免费代理IP

1.创建项目 scrapy startproject getProxy 2.创建spider文件,抓取www.proxy360.cn www.xicidaili.com两个代理网站内容 cd项目的spiders模块下执行 scrapy genspider proxy360Spider proxy360.cn scrapy genspider xiciSpider xicidaili.com scrapy shell http://www.proxy360.cn/Region/China scra

PHP简单爬虫 爬取免费代理ip 一万条

目标站:http://www.xicidaili.com/ 代码: <?php require 'lib/phpQuery.php'; require 'lib/QueryList.php'; require "db/shared/ez_sql_core.php"; require "db/mysql/ez_sql_mysql.php"; require "public/function.php"; use QL\QueryList; //

简单爬虫-爬取免费代理ip

环境:python3.6 主要用到模块:requests,PyQuery 代码比较简单,不做过多解释了 #!usr/bin/python # -*- coding: utf-8 -*- import requests from pyquery import PyQuery as pq class GetProxy(object): def __init__(self): # 代理ip网站 self.url = 'http://www.xicidaili.com/nn/' self.header

Python 爬虫抓取代理IP,并检测联通性

帮朋友抓了一些代理IP,并根据测试联的通性,放在了不通的文件夹下.特将源码分享 注意: 1,环境Python3.5 2,安装BeautifulSoup4  requests 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 5

python爬虫抓取站长之家IP库,仅供练习用!

python爬虫抓取站长之家IP库,单线程的,仅供练习,IP库数据有43亿条,如果按此种方法抓取至少得数年,所以谨以此作为练手,新手代码很糙,请大家见谅. #!/usr/bin/python #coding=UTF-8 import urllib2 import re import os import csv import codecs user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = { 'User-

php爬虫抓取信息及反爬虫相关

58爬虫了百姓,赶集和58互爬,最后各种信息相同,都是爬虫后的数据库调用,潜规则啊,几家独大还暗中各种攻击,赶驴网的幽默事例我不想多评价.这个时代是砸.钱*养.钱的时代,各种姚晨杨幂葛优,各种地铁公车广告,各种卫视广告,铺天盖地~~~ 来谈php爬虫抓取信息~~ php爬虫首推Curl函数了,先来认识下它. 0x01.curl扩展的安装: 1.确保php子文件夹ext里面有php_curl.dll(一般都有的,一般配置时候会设置环境变量的) 2.将php.ini里面的;extension=php

Python实现爬取可用代理IP

在实现爬虫时,动态设置代理IP可以有效防止反爬虫,但对于普通爬虫初学者需要在代理网站上测试可用代理IP.由于手动测试过程相对比较繁琐,且重复无用过程故编写代码以实现动态抓取可用的代理IP.动态代理IP保存在Json文件中,以供后续具体项目爬虫使用,但所爬取的代理IP是免费IP,所以可能出现当时爬取能用,过一段时间无法使用的情况. 1) 先从西刺代理网站上爬取前10页,速度较快的IP,保存到proxies数组中,其中proxy使用的是requests.get()可直接使用字典格式 1 print(

python 爬虫抓取心得

quanwei9958 转自 python 爬虫抓取心得分享 urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quote('要编码的字符串') query = urllib.quote(singername) url = 'http://music.baidu.com/search?key='+query response = urllib.urlopen(url) text = response.read()

python 爬虫抓取心得分享

/** author: insun title:python 爬虫抓取心得分享 blog:http://yxmhero1989.blog.163.com/blog/static/112157956201311821444664/ **/    0x1.urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quote('要编码的字符串') query = urllib.quote(singername) url = 'h