class ProxyDownloaderMiddleware(object):
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the downloader middleware does not modify the
# passed objects.
def __init__(self):
self.request_proxy_url = ""
self.IpPool = Queue() # 维护代理IP池
self.Ipset = set() # 记录已经取到的代理IP
self.request_proxry(number=5) def request_proxry(self, number=5):
"""
:param number: the number of getting proxies
:return:
"""
if self.IPportQueue.qsize() > 8:
return
url = 'https://dps.kdlapi.com/api/getdps/?orderid=987658645908252&num=%d&pt=1&dedup=1&format=json&sep=1' % number
# lock.acquire()
r = requests.get(url)
# lock.release()
dc = r.json()
# print(dc,'123')
for item in dc['data']['proxy_list']:
if item in self.IPset:
continue
self.IPportQueue.put({'ipport': item, 'useTimes': 0})
self.IPset.add(item)
print(item, '+++++++++++++++++') def get_proxy_ip(self):
item = self.IpPool.get()
item["useTimes"] += 1
if item["useTimes"] > 10:
self.request_proxry(number=2)
else:
self.IpPool.put(item)
return "https://" + item["ip_port"] @classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s def process_request(self, request, spider):
# Called for each request that goes through the downloader
# middleware. # Must either:
# - return None: continue processing this request
# - or return a Response object
# - or return a Request object
# - or raise IgnoreRequest: process_exception() methods of
# installed downloader middleware will be called
request.meta["proxy"] = self.get_proxy_ip() return None def process_response(self, request, response, spider):
# Called with the response returned from the downloader. # Must either;
# - return a Response object
# - return a Request object
# - or raise IgnoreRequest
return response def process_exception(self, request, exception, spider):
# Called when a download handler or a process_request()
# (from other downloader middleware) raises an exception. # Must either:
# - return None: continue processing this exception
# - return a Response object: stops process_exception() chain
# - return a Request object: stops process_exception() chain
pass def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name)

scrapy爬虫-代理IP中间件的更多相关文章

  1. scrapy的User-Agent中间件、代理IP中间件、cookies设置、多个爬虫自定义settings设置

    在scrapy的反爬中,常用的几个配置,简单总结了下: User-Agent中间件: from fake_useragent import UserAgent class RandomUserAgen ...

  2. scrapy 使用代理ip

    1.在settings文件中添加ip池 IPPOOL=['xxx.xx.xx.xx','xxx.xx.xxx.xx'] 2.在middleware文件中添加自己的代理ip类(首先需要导入ipPOOL, ...

  3. 通过爬虫代理IP快速增加博客阅读量——亲测CSDN有效!

    写在前面 题目所说的并不是目的,主要是为了更详细的了解网站的反爬机制,如果真的想要提高博客的阅读量,优质的内容必不可少. 了解网站的反爬机制 一般网站从以下几个方面反爬虫: 1. 通过Headers反 ...

  4. 【python3】如何建立爬虫代理ip池

    一.为什么需要建立爬虫代理ip池 在众多的网站防爬措施中,有一种是根据ip的访问频率进行限制的,在某段时间内,当某个ip的访问量达到一定的阀值时,该ip会被拉黑.在一段时间内被禁止访问. 这种时候,可 ...

  5. 维护爬虫代理IP池--采集并验证

    任务分析 我们爬的免费代理来自于https://www.kuaidaili.com这个网站.用`requests`将ip地址与端口采集过来,将`IP`与`PORT`组合成`requests`需要的代理 ...

  6. 建立爬虫代理IP池

    单线程构建爬虫代理IP池 #!/usr/bin/python3.5 # -*- coding:utf-8 -*- import time import tempfile from lxml impor ...

  7. 写了个爬虫代理ip的脚本给大家使用

    写了个爬虫代理ip的脚本给大家使用 一.代码 import requests from lxml.html import etree url = 'http://www.kuaidaili.com/f ...

  8. 可能是一份没什么用的爬虫代理IP指南

    写在前面 做爬虫的小伙伴一般都绕不过代理IP这个问题. PS:如果还没遇到被封IP的场景,要不就是你量太小人家懒得理你,要不就是人家压根不在乎... 爬虫用户自己是没有能力维护一系列的代理服务器和代理 ...

  9. Python爬虫代理IP池

    目录[-] 1.问题 2.代理池设计 3.代码模块 4.安装 5.使用 6.最后 在公司做分布式深网爬虫,搭建了一套稳定的代理池服务,为上千个爬虫提供有效的代理,保证各个爬虫拿到的都是对应网站有效的代 ...

随机推荐

  1. python摄像头

    import cv2 cap = cv2.VideoCapture(0) i = 0 while 1: ret, frame = cap.read() k = cv2.waitKey(1) if k ...

  2. vs strcore.cpp(156) 内存泄漏

    在一个静态函数的线程回调中,一个cstring的对象没释放. 遇到这种问题: 1.查看所有的指针对象,有没有合适的回收 2.查看代码的malloc,看看有没有free 3.一点一点注释代码,定位位置

  3. zabbix-agent服务无法启动

    zabbix-agent服务无法启动解决方案1.先配置yum源2.卸载已经安装的zabbix-agent3.重新安装zabbix-agent4.配置zabbix-agent配置文件: Server=服 ...

  4. 项目课 day01

    ## 电商 ### 定义 指在互联网(Internet).内部网(Intranet)和增值网(VAN,Value Added Network)上以电子交易方式进行交易活动和相关服务活动 ### 电商的 ...

  5. 国内外主流的三维GIS软件

    我国GIS经过三十多年的发展,理论和技术日趋成熟,在传统二维GIS已不能满足应用需求的情况下,三维GIS应运而生,并成为GIS的重要发展方向之一.上世纪八十年代末以来,空间信息三维可视化技术成为业界研 ...

  6. CentOS-TFTP服务搭建

    title date tags layout CentOS6.5 TFTP搭建 2018-08-26 Centos6.5服务器搭建 post 1.安装TFTP服务 yum install tftp-s ...

  7. iOS仿抖音节拍界面、Swift,MVVM架构完整项目、日历demo、滚动切换分类等源码

    iOS精选源码 在Object-C中学习数据结构与算法之排序算法 日历-基本功能都有的日历 选择日期 上下月 动画 仿抖音卡节拍界面 垂直.水平方向皆可滚动.header悬浮的列表视图 Auto La ...

  8. 利用数目找中位数(牛客第七场E)

    https://ac.nowcoder.com/acm/contest/887/E 树状数组做法(代码有注释) #include<bits/stdc++.h> using namespac ...

  9. derby数据库

    derby数据库 https://www.cnblogs.com/zuzZ/p/8107915.html Derby数据库的使用 https://www.cnblogs.com/wkfvawl/p/1 ...

  10. django框架基础-django模板语言-长期维护

    ##################     常用的模板语言       ####################### # django的模板语言, # # 只需要记两种特殊符号: # # {{ } ...