时间:2021-05-22
一、为什么要搭建爬虫代理池
在众多的网站防爬措施中,有一种是根据ip的访问频率进行限制,即在某一时间段内,当某个ip的访问次数达到一定的阀值时,该ip就会被拉黑、在一段时间内禁止访问。
应对的方法有两种:
1. 降低爬虫的爬取频率,避免IP被限制访问,缺点显而易见:会大大降低爬取的效率。
2. 搭建一个IP代理池,使用不同的IP轮流进行爬取。
二、搭建思路
1、从代理网站(如:西刺代理、快代理、云代理、无忧代理)爬取代理IP;
2、验证代理IP的可用性(使用代理IP去请求指定URL,根据响应验证代理IP是否生效);
3、将可用的代理IP保存到数据库;
在《Python爬虫代理池搭建》一文中我们已经使用Python的 requests 模块简单实现了一个IP代理池搭建,但是爬取速度较慢。由于西刺代理、快代理和云代理等网站需要爬取的IP代理列表页多达上千页,使用此种方法来爬取其实并不适合。
本文将以快代理网站的IP代理爬取为例,示例如何使用 Scrapy-Redis 来爬取代理IP。
三、搭建代理池
scrapy 项目的目录结构如下:
items.py
# -*- coding: utf-8 -*-import reimport scrapyfrom proxy_pool.settings import PROXY_URL_FORMATTERschema_pattern = re.compile(r'http|https$', re.I)ip_pattern = re.compile(r'^([0-9]{1,3}.){3}[0-9]{1,3}$', re.I)port_pattern = re.compile(r'^[0-9]{2,5}$', re.I)class ProxyPoolItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() ''' { "schema": "http", # 代理的类型 "ip": "127.0.0.1", # 代理的IP地址 "port": "8050", # 代理的端口号 "original":"西刺代理", "used_total": 11, # 代理的使用次数 "success_times": 5, # 代理请求成功的次数 "continuous_failed": 3, # 使用代理发送请求,连续失败的次数 "created_time": "2018-05-02" # 代理的爬取时间 } ''' schema = scrapy.Field() ip = scrapy.Field() port = scrapy.Field() original = scrapy.Field() used_total = scrapy.Field() success_times = scrapy.Field() continuous_failed = scrapy.Field() created_time = scrapy.Field() # 检查IP代理的格式是否正确 def _check_format(self): if self['schema'] is not None and self['ip'] is not None and self['port'] is not None: if schema_pattern.match(self['schema']) and ip_pattern.match(self['ip']) and port_pattern.match( self['port']): return True return False # 获取IP代理的url def _get_url(self): return PROXY_URL_FORMATTER % {'schema': self['schema'], 'ip': self['ip'], 'port': self['port']}kuai_proxy.py
# -*- coding: utf-8 -*-import reimport timeimport scrapyfrom proxy_pool.utils import strip, loggerfrom proxy_pool.items import ProxyPoolItemclass KuaiProxySpider(scrapy.Spider): name = 'kuai_proxy' allowed_domains = ['kuaidaili.com'] start_urls = ['https://' + next_page yield scrapy.Request(next_url)middlewares.py
# -*- coding: utf-8 -*-import randomfrom proxy_pool.utils import logger# 随机选择 IP 代理下载器中间件class RandomProxyMiddleware(object): # 从 settings 的 PROXIES 列表中随机选择一个作为代理 def process_request(self, request, spider): proxy = random.choice(spider.settings['PROXIES']) request.meta['proxy'] = proxy return None# 随机选择 User-Agent 的下载器中间件class RandomUserAgentMiddleware(object): def process_request(self, request, spider): # 从 settings 的 USER_AGENTS 列表中随机选择一个作为 User-Agent user_agent = random.choice(spider.settings['USER_AGENT_LIST']) request.headers['User-Agent'] = user_agent return None def process_response(self, request, response, spider): # 验证 User-Agent 设置是否生效 logger.info("headers ::> User-Agent = " + str(request.headers['User-Agent'], encoding="utf8")) return responsepipelines.py
# -*- coding: utf-8 -*-import jsonimport redisfrom proxy_pool.settings import REDIS_HOST,REDIS_PORT,REDIS_PARAMS,PROXIES_UNCHECKED_LIST,PROXIES_UNCHECKED_SETserver = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT,password=REDIS_PARAMS['password'])class ProxyPoolPipeline(object): # 将可用的IP代理添加到代理池队列 def process_item(self, item, spider): if not self._is_existed(item): server.rpush(PROXIES_UNCHECKED_LIST, json.dumps(dict(item),ensure_ascii=False)) # 检查IP代理是否已经存在 def _is_existed(self,item): added = server.sadd(PROXIES_UNCHECKED_SET, item._get_url()) return added == 0settings.py
# -*- coding: utf-8 -*-BOT_NAME = 'proxy_pool'SPIDER_MODULES = ['proxy_pool.spiders']NEWSPIDER_MODULE = 'proxy_pool.spiders'# 保存未检验代理的Redis keyPROXIES_UNCHECKED_LIST = 'proxies:unchecked:list'# 已经存在的未检验HTTP代理和HTTPS代理集合PROXIES_UNCHECKED_SET = 'proxies:unchecked:set'# 代理地址的格式化字符串PROXY_URL_FORMATTER = '%(schema)s://%(ip)s:%(port)s'# 通用请求头字段DEFAULT_REQUEST_HEADERS = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7', 'Connection': 'keep-alive'}# 请求太频繁会导致 503 ,在此设置 5 秒请求一次DOWNLOAD_DELAY = 5 # 250 ms of delayUSER_AGENT_LIST = [ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1", "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5", "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24"]# Obey robots.txt rulesROBOTSTXT_OBEY = False# Enable or disable downloader middlewares# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.htmlDOWNLOADER_MIDDLEWARES = { 'proxy_pool.middlewares.RandomUserAgentMiddleware': 543, # 'proxy_pool.middlewares.RandomProxyMiddleware': 544,}ITEM_PIPELINES = { 'proxy_pool.pipelines.ProxyPoolPipeline': 300,}PROXIES = [ "https://171.13.92.212:9797", "https://164.163.234.210:8080", "https://143.202.73.219:8080", "https://103.75.166.15:8080"]####################################################################下面是Scrapy-Redis相关配置####################################################################### 指定Redis的主机名和端口REDIS_HOST = '172.16.250.238'REDIS_PORT = 6379REDIS_PARAMS = {'password': '123456'}# 调度器启用Redis存储Requests队列SCHEDULER = "scrapy_redis.scheduler.Scheduler"# 确保所有的爬虫实例使用Redis进行重复过滤DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"# 将Requests队列持久化到Redis,可支持暂停或重启爬虫SCHEDULER_PERSIST = True# Requests的调度策略,默认优先级队列SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.PriorityQueue'utils.py
# -*- coding: utf-8 -*-import logging# 设置日志输出格式logging.basicConfig(level=logging.INFO, format='[%(asctime)-15s] [%(levelname)8s] [%(name)10s ] - %(message)s (%(filename)s:%(lineno)s)', datefmt='%Y-%m-%d %T' )logger = logging.getLogger(__name__)# Truncate header and tailer blanksdef strip(data): if data is not None: return data.strip() return data到此这篇关于详解基于Scrapy的IP代理池搭建的文章就介绍到这了,更多相关Scrapy IP代理池搭建内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
简介提到爬虫,大部分人都会想到使用Scrapy工具,但是仅仅停留在会使用的阶段。为了增加对爬虫机制的理解,我们可以手动实现多线程的爬虫过程,同时,引入IP代理池
在进行数据抓取时,经常会遇到IP被限制的情况,常见的解决方案是搭建代理IP池,或购买IP代理的服务。除此之外,还有一个另外的方法就是使用家里的宽带网络进行抓取。
在公司做分布式深网爬虫,搭建了一套稳定的代理池服务,为上千个爬虫提供有效的代理,保证各个爬虫拿到的都是对应网站有效的代理IP,从而保证爬虫快速稳
在公司做分布式深网爬虫,搭建了一套稳定的代理池服务,为上千个爬虫提供有效的代理,保证各个爬虫拿到的都是对应网站有效的代理IP,从而保证爬虫快速稳定的运行,当然在
一、为什么要搭建爬虫代理池在众多的网站防爬措施中,有一种是根据ip的访问频率进行限制,即在某一时间段内,当某个ip的访问次数达到一定的阀值时,该ip就会被拉黑、