二十七 Python分布式爬虫打造搜索引擎Scrapy精讲—通过自定义中间件全局随机更换代理IP
设置代理ip只需要,自定义一个中间件,重写process_request方法,
request.meta['proxy'] = "http://185.82.203.146:1080" 设置代理IP
中间件,注意将中间件注册到配置文件里去

from adc.daili_ip.sh_yong_ip.sh_yong_ip import sui_ji_hq_ip from fake_useragent import UserAgent #导入浏览器用户代理模块 class RequestsUserAgentmiddware(object): #自定义浏览器代理中间件
#中间件随机更换Requests请求头信息的User-Agent浏览器用户代理
def __init__(self,crawler):
super(RequestsUserAgentmiddware, self).__init__() #获取上一级父类基类的,__init__方法里的对象封装值
self.ua = UserAgent() #实例化浏览器用户代理模块类
self.ua_type = crawler.settings.get('RANDOM_UA_TYPE','random') #获取settings.py配置文件里的RANDOM_UA_TYPE配置的浏览器类型,如果没有,默认random,随机获取各种浏览器类型 @classmethod #函数上面用上装饰符@classmethod,函数里有一个必写形式参数cls用来接收当前类名称
def from_crawler(cls, crawler): #重载from_crawler方法
return cls(crawler) #将crawler爬虫返回给类 def process_request(self, request, spider): #重载process_request方法
def get_ua(): #自定义函数,返回浏览器代理对象里指定类型的浏览器信息
return getattr(self.ua, self.ua_type)
sssf = get_ua()
print('启用用户代理浏览器信息:{0}'.format(sssf))
request.headers.setdefault('User-Agent', get_ua()) #将浏览器代理信息添加到Requests请求 class MyproxiesSpiderMiddleware(object):
#中间件随机更换IP def process_request(self, request, spider): #重写process_request方法
#到数据库随机获取一个IP xieyi = request._get_url() #_get_url可以获取到请求URL,来判断是什么协议请求如https
print(xieyi)
dai_ip = sui_ji_hq_ip('http') #到数据库随机获取一个代理IP
request.meta['proxy'] = "http://{0}".format(dai_ip) #字符串格式化设置代理IP #request.meta['proxy'] = "http://185.82.203.146:1080" 设置代理IP

随机数据库获取IP

#!/usr/bin/env python
# -*- coding:utf8 -*-
import time import requests from adc.daili_ip.mysq import shujuku as ORM def suiji_ip(rst):
"""
调用此函数随机到数据库获取代理IP返回IP,如果IP不可用会自动删除返回False
"""
atime = time.localtime(time.time()-240) #设置获取多少时间以内检测过的IP(单位秒)
sudu = '00:00:03' #设置获取访问速度小于等于多少的IP,单位(时分秒)默认3秒
dqatime = "{0}-{1}-{2} {3}:{4}:{5}".format(
atime.tm_year,
atime.tm_mon,
atime.tm_mday,
atime.tm_hour,
atime.tm_min,
atime.tm_sec
) # 将格式化时间日期,单独取出来拼接成一个完整日期 try:
mysq = ORM.session()
shuju = mysq.query(
ORM.daili_ip.ip,
ORM.daili_ip.port,
ORM.daili_ip.xtype,
ORM.daili_ip.seshi_ri_qi,
ORM.daili_ip.connectTimeMs
).from_statement(
"SELECT ip,port,xtype,seshi_ri_qi,connectTimeMs FROM daili_ip WHERE xtype='{0}' AND ce_shi='{1}' AND seshi_ri_qi>='{2}' AND connectTimeMs<='{3}' ORDER BY RAND() LIMIT 1".format(rst, '1', dqatime, sudu)
).all()
mysq.close()
if shuju:
print('获取到IP')
else:
print('获取IP失败,请检查获取条件')
except Exception as e:
print('查询代理IP数据出错')
return True
ip = shuju[0][0]
duan_kou = shuju[0][1]
print('启用代理IP,数据库获取到IP:{0}'.format(shuju)) http_url = '{0}://image.baidu.com/'.format(rst)
proxy_url = '{0}://{1}:{2}'.format(rst, ip, duan_kou)
headers = {
'Referer': http_url,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0',
} print('启用代理IP,测试网址:{0}'.format(http_url))
print('启用代理IP,测试头:{0}'.format(proxy_url))
try:
proxy_dict = {
'http': proxy_url
}
response = requests.get(http_url, proxies=proxy_dict, headers=headers)
except Exception as e:
print('启用代理IP,测速连接失败{0}'.format(e))
print('启用代理IP,测速连接失败,当前IP不可用,删除当前ip!')
fanhui = mysq.query(ORM.daili_ip).filter(ORM.daili_ip.ip == ip).delete() # 删除不可以数据
mysq.commit()
mysq.close()
if fanhui == 1:
print("成功删除当前IP")
else:
print('删除当前IP失败')
return False
else:
code = response.status_code # 获取状态吗
sudu = str(response.elapsed) # 获取响应时间
if code >= 200 and code < 300:
atime = time.localtime()
dqatime = "{0}-{1}-{2} {3}:{4}:{5}".format(
atime.tm_year,
atime.tm_mon,
atime.tm_mday,
atime.tm_hour,
atime.tm_min,
atime.tm_sec
) # 将格式化时间日期,单独取出来拼接成一个完整日期 print('启用代理IP,测试代理ip--{0}{1}--状态可用--状态码--{2}'.format(ip, duan_kou, code))
print('启用代理IP,当前IP可以,正在向数据库标记')
fanhui = mysq.query(ORM.daili_ip).filter(ORM.daili_ip.ip == ip).update({
"ce_shi": "1",
"seshi_ri_qi": dqatime,
"connectTimeMs": sudu
})
mysq.commit()
mysq.close()
if fanhui == 1:
print('向数据库成功标记可用IP!')
else:
print('向数据库标记可用IP失败!!!')
print('向爬虫返回IP:{0}:{1}'.format(ip, duan_kou))
return ip + ':' + duan_kou
else:
print('启用代理IP,测试代理ip--{0}{1}--状态不可用--状态码--{2}'.format(ip, duan_kou, code))
print('返回状态码不可以,正在向数据库删除当前IP')
fanhui = mysq.query(ORM.daili_ip).filter(ORM.daili_ip.ip == ip).delete() # 删除不可以数据
mysq.commit()
mysq.close()
if fanhui == 1:
print('删除当前IP成功')
else:
print('删除当前IP失败')
return False def sui_ji_hq_ip(rst):
"""
正式使用:调用此函数,接收一个参数协议,如http
循环到数据库获取IP,IP如果不可用删除后继续获取,直到ip可以后返回ip
值循环获取测试30分钟内有效的IP
"""
n = True
h = None
while n:
youxiao_ip = suiji_ip(rst)
if youxiao_ip:
h = youxiao_ip
n = False
return h # print(sui_ji_hq_ip('http'))

数据库模块文件

import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index,text,DATETIME,TIME
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import create_engine import requests
import json
import time
import datetime #配置数据库引擎信息
ENGINE = create_engine("mysql+pymysql://root:279819@127.0.0.1:3306/cshi?charset=utf8", max_overflow=500, echo=True) Base = declarative_base() #创建一个SQLORM基类 class daili_ip(Base): #ip池设计表
__tablename__ = 'daili_ip' id = Column(Integer, primary_key=True, autoincrement=True)
ip = Column(String(300), unique=True) #IP
port = Column(String(300)) #端口
city = Column(String(300)) #城市
isp = Column(String(300)) #运营商
connectTimeMs = Column(TIME()) #速度
anonymity = Column(String(300)) #匿名方式
country = Column(String(300)) #国家
xtype = Column(String(300)) #协议
zhuang_tai_ma = Column(String(300)) #状态码
ruku_riqi = Column(DATETIME()) #入库日期
ce_shi = Column(String(300)) #测试状态
seshi_ri_qi = Column(DATETIME()) #测试日期
shi_xiao_riqi = Column(DATETIME()) # 失效日期 def init_db():
Base.metadata.create_all(ENGINE) #向数据库创建指定表 def drop_db():
Base.metadata.drop_all(ENGINE) #向数据库删除指定表 def session():
cls = sessionmaker(bind=ENGINE) #创建sessionmaker类,操作表
return cls() # drop_db() #删除表
# init_db()

二十七 Python分布式爬虫打造搜索引擎Scrapy精讲—通过自定义中间件全局随机更换代理IP的更多相关文章
- 第三百四十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—通过自定义中间件全局随机更换代理IP
第三百四十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—通过自定义中间件全局随机更换代理IP 设置代理ip只需要,自定义一个中间件,重写process_request方法, request ...
- 第三百四十七节,Python分布式爬虫打造搜索引擎Scrapy精讲—通过downloadmiddleware中间件全局随机更换user-agent浏览器用户代理
第三百四十七节,Python分布式爬虫打造搜索引擎Scrapy精讲—通过downloadmiddleware随机更换user-agent浏览器用户代理 downloadmiddleware介绍中间件是 ...
- 二十六 Python分布式爬虫打造搜索引擎Scrapy精讲—通过downloadmiddleware中间件全局随机更换user-agent浏览器用户代理
downloadmiddleware介绍中间件是一个框架,可以连接到请求/响应处理中.这是一种很轻的.低层次的系统,可以改变Scrapy的请求和回应.也就是在Requests请求和Response响应 ...
- 三十七 Python分布式爬虫打造搜索引擎Scrapy精讲—将bloomfilter(布隆过滤器)集成到scrapy-redis中
Python分布式爬虫打造搜索引擎Scrapy精讲—将bloomfilter(布隆过滤器)集成到scrapy-redis中,判断URL是否重复 布隆过滤器(Bloom Filter)详解 基本概念 如 ...
- 二十三 Python分布式爬虫打造搜索引擎Scrapy精讲—craw母版l创建自动爬虫文件—以及 scrapy item loader机制
用命令创建自动爬虫文件 创建爬虫文件是根据scrapy的母版来创建爬虫文件的 scrapy genspider -l 查看scrapy创建爬虫文件可用的母版 Available templates: ...
- 四十七 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现搜索的自动补全功能
elasticsearch(搜索引擎)提供了自动补全接口 官方说明:https://www.elastic.co/guide/en/elasticsearch/reference/current/se ...
- 二十一 Python分布式爬虫打造搜索引擎Scrapy精讲—爬虫数据保存
注意:数据保存的操作都是在pipelines.py文件里操作的 将数据保存为json文件 spider是一个信号检测 # -*- coding: utf-8 -*- # Define your ite ...
- 二十 Python分布式爬虫打造搜索引擎Scrapy精讲—编写spiders爬虫文件循环抓取内容—meta属性返回指定值给回调函数—Scrapy内置图片下载器
编写spiders爬虫文件循环抓取内容 Request()方法,将指定的url地址添加到下载器下载页面,两个必须参数, 参数: url='url' callback=页面处理函数 使用时需要yield ...
- 十七 Python分布式爬虫打造搜索引擎Scrapy精讲—深度优先与广度优先原理
网站树形结构 深度优先 是从左到右深度进行爬取的,以深度为准则从左到右的执行(递归方式实现)Scrapy默认是深度优先的 广度优先 是以层级来执行的,(列队方式实现)
随机推荐
- django 用户注册功能实现
增加views的类 class RegisterView(View): def get(self, request): return render(request, 'register.html', ...
- Full List of NMD R1 Singapore 2017
The initial adidas NMD Singapore, which sparked a sneaker craze larger than the Three Stripes could' ...
- Django restful Framework 之Requests and Response 方法
前言: 本章主要介绍REST framework 内置的必要的功能. Request objects Response objects Status codes Wrapping API views ...
- iOS 提升代码的安全性,可以做哪些措施???
希望能尽量防止别人 反编译你的代码: 目前苹果审核规则可知,苹果官方是不希望你使用代码混淆的...如果发现了你用代码混淆,甚至会勒令你修改你的代码,否则下一次审核会直接移除你的app…尤其是跑脚本的那 ...
- NodeJS NPM HTTPS
npm config set registry http://registry.npmjs.org/
- ThreadLocal的设计理念与作用
转自:http://www.iteye.com/topic/103804 首先,ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线 ...
- [转]将Eclipse设置为黑色主题 方式一
将Eclipse设置为黑色主题 觉得黑色的主题&配色很高大上,于是花了点时间实践出下面一种方法. 修改代码编辑区配色 修改整个软件主题 先上成果图: 但是进度条依旧是白色的,不知道怎么弄了╮( ...
- rowspan && colspan
> 跨行 <html> <body> <table width="> <tr> <th>col1</th> &l ...
- Spring使用事务
Spring使用事务,一共有4个步骤 1.配置数据源 例如: <bean id="dataSource" class="com.mchange.v2.c3p0.Co ...
- 20145312 实验二《 Java面向对象程序设计》
20145312 实验二< Java面向对象程序设计> 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解 ...