scrapy中间件中使用selenium切换ip
scrapy抓取一些需要js加载页面时一般要么是通过接口直接获取数据,要么是js加载,但是我通过selenium也可以获取动态页面
但是有个问题,容易给反爬,因为在scrapy中间件mid中使用selenium的ip不会跟着你在中间件中切换的ip变化,还是使用本机的ip在访问网站,
这里通过 确定网页url进行过滤,什么网页使用selenium,什么使用scrapy自带的抓取,
为selenium单独设置一个获取ip的办法,当然也可以使用全局变量
from selenium import webdriver
from scrapy.http.response.html import HtmlResponse
from selenium.webdriver.chrome.options import Options
import json
import requests class ip_mid(object):
def __init__(self):
self.ip = ''
self.url = 'http://proxy.1again.cc:35050/api/v1/proxy/?type=2'
self.ip_num = 0 def process_request(self,request,spider):
# if re.findall(r'根据需求的url过滤,简单的页面', request.url):
print('正在使用ip')
if self.ip_num ==0 or self.ip_num >=10:
res = json.loads(requests.get(url=self.url).content.decode())
if res:
ip = res['data']['proxy']
print(ip,'-'*20)
self.ip = ip
print(self.ip)
self.ip_num = 1 if self.ip:
request.meta['proxy'] = 'http://' + self.ip
self.ip_num += 1
print('ip地址>>>{} --- 使用次数{}'.format(self.ip, self.ip_num))
else:
self.ip_num += 3
print('使用的是本机ip......') '''
两个获取ip的设置,对ip池的访问返回是个问题,
如果加上一个判定什么网页使用selenium + 获取ip,什么网页使用正常的获取ip正常的访问
比如 if re.findall(r'根据需求的url过滤,必须使用selenium加载的js动态页面',request.url)
'''
class YanzhenIp_selenium_DownloaderMiddleware(object): def __init__(self): self.chrome_options = Options()
self.chrome_options.add_argument('--headless')
self.chrome_options.add_argument('--disable-gpu')
# self.driver = webdriver.Chrome(chrome_options=chrome_options)
self.chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) self.ip = ''
self.url = 'http://proxy.1again.cc:35050/api/v1/proxy/?type=2'
self.ip_num = 0 def process_request(self, request, spider):
# if re.findall(r'根据需求的url过滤,必须使用selenium加载的js动态页面', request.url):
print('获取ip............') # 为selenium获取ip
if self.ip_num == 0 or self.ip_num >= 3:
res = json.loads(requests.get(url=self.url).content.decode())
if res:
ip = res['data']['proxy']
self.ip = ip
self.ip_num = 1 print('调用selenium中.............') self.chrome_options.add_argument("--proxy-server=http://{}".format(self.ip)) # 加载ip
print('插入ip{},并使用{}'.format(self.ip,self.ip_num), '-' * 20)
self.driver = webdriver.Chrome(chrome_options=self.chrome_options) self.driver.get(request.url)
html = self.driver.page_source
url = self.driver.current_url response = HtmlResponse(url=url,body=html,encoding='utf-8',request=request)
return response def close_spider(self,spider):
self.driver.close()
print('关闭selenium')
ua也可以这样搞
随手一写,有待优化
ga改进版本,有待优化
class YanzhenIp_selenium_DownloaderMiddleware(object):
def __init__(self):
self.chrome_options = Options()
self.chrome_options.add_argument('--headless')
self.chrome_options.add_argument('--disable-gpu')
# self.driver = webdriver.Chrome(chrome_options=chrome_options)
self.chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
self.ip = ''
self.url = 'http://proxy.1again.cc:35050/api/v1/proxy/?type=2'
self.ip_num = 0
def process_request(self, request, spider):
# if re.findall('js加载页面的url',request.url):
if self.ip_num == 0 or self.ip_num >= 10:
res = json.loads(requests.get(url=self.url).content.decode())
if res:
ip = res['data']['proxy']
self.ip = ip
self.ip_num = 1
if re.findall('js加载页面的url', request.url):
# TODO if self.ip ?? 做个判断有咩有ip 没有的时候怎么办
print('调用selenium中.............')
self.chrome_options.add_argument("--proxy-server=http://{}".format(self.ip))
print('插入ip{},并使用{}'.format(self.ip,self.ip_num), '-' * 20)
self.driver = webdriver.Chrome(chrome_options=self.chrome_options)
self.driver.get(request.url)
html = self.driver.page_source
url = self.driver.current_url
response = HtmlResponse(url=url,body=html,encoding='utf-8',request=request)
return response
else: # 抓取简单的页面,scrapy可以胜任
if self.ip:
request.meta['proxy'] = 'http://' + self.ip
self.ip_num += 1
print('ip地址>>>{} --- 使用次数{}'.format(self.ip, self.ip_num))
else:
self.ip_num += 3
print('使用的是本机ip......')
def close_spider(self,spider):
self.driver.close()
print('关闭selenium')
scrapy中间件中使用selenium切换ip的更多相关文章
- scrapy中间件中发送邮件
背景介绍:之前写过通过通过scrapy的扩展发送邮件,在爬虫关闭的时候发送邮件.那个时候有个问题就是MailSender对象需要return出去.这次需要在中间件中发送邮件,但是中间件中不能随便使用r ...
- Scrapy中间件user-agent和ip代理使用
一.定义实现随机User-Agent的下载中间件 1.在middlewares.py中完善代码 import random from Tencent.settings import USER_AGEN ...
- 在Scrapy中使用selenium
在scrapy中使用selenium 在scrapy中需要获取动态加载的数据的时候,可以在下载中间件中使用selenium 编码步骤: 在爬虫文件中导入webdrvier类 在爬虫文件的爬虫类的构造方 ...
- scrapy——中间件UserAgent代理
pip install fake-useragent 使用说明:from fake_useragent import UserAgent# 实例化一个UserAgent对象ua = UserAgent ...
- Scrapy中集成selenium
面对众多动态网站比如说淘宝等,一般情况下用selenium最好 那么如何集成selenium到scrapy中呢? 因为每一次request的请求都要经过中间件,所以写在中间件中最为合适 from se ...
- 第三百五十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—将selenium操作谷歌浏览器集成到scrapy中
第三百五十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—将selenium操作谷歌浏览器集成到scrapy中 1.爬虫文件 dispatcher.connect()信号分发器,第一个参数信 ...
- scrapy中的selenium
引入 在通过scrapy框架进行某些网站数据爬取的时候,往往会碰到页面动态数据加载的情况发生,如果直接使用scrapy对其url发请求,是绝对获取不到那部分动态加载出来的数据值.但是通过观察我们会发现 ...
- scrapy中使用selenium来爬取页面
scrapy中使用selenium来爬取页面 from selenium import webdriver from scrapy.http.response.html import HtmlResp ...
- 如何优雅的在scrapy中使用selenium —— 在scrapy中实现浏览器池
1 使用 scrapy 做采集实在是爽,但是遇到网站反爬措施做的比较好的就让人头大了.除了硬着头皮上以外,还可以使用爬虫利器 selenium,selenium 因其良好的模拟能力成为爬虫爱(cai) ...
随机推荐
- confessed to doing|conform|confined|entitle|
to admit that you have done something wrong or something that you feel guilty or bad about 坦白:供认,招认: ...
- SHELL小练习
1.SHELL编程服务器IP修改脚本 脚本实现动态IP修改: 脚本实现静态IP修改: 实现IP地址输入判断正确性: IP地址修改成功判断&回滚: 2.SHELL编程Tomcat多实例管理脚本( ...
- [LC] 100. Same Tree
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- C语言Windows程序设计—— 使用计时器
传统意义上的计时器是指利用特定的原理来测量时间的装置, 在古代, 常用沙漏.点燃一炷香等方式进行粗略的计时, 在现代科技的带动下, 计时水平越来越高, 也越来越精确, 之所以需要进行计时是在很多情况下 ...
- top100 paper
- hql错误:No data type for node: org.hibernate.hql.ast.tree.IdentNode
今天写了一个查询,用的是hql,数据库是mysql.多表联查,结果报错了报: \-[IDENT] IdentNode: 'routerNumber' {originalText=routerNumbe ...
- 吴裕雄--天生自然python编程:turtle模块绘图(1)
Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行 ...
- tomcat——nginx负载均衡
Tomcat一般应用在这种小型系统中应用非常广泛,是开发调试jsp的首先应用.Tomcat和其他web软甲一样具有解析HTML语言的功能,但是处理效率远不及Apacge和Nginx,所以Tomcat一 ...
- Waymo
技术优势 Waymo在自己的激光雷达系统上投入了大量资金,它认为这项技术对自动驾驶汽车的长期成功至关重要.实际上,该公司声称它已经将专有激光雷达传感器的成本降低了90%,这种传感器以前的制造成本为7. ...
- JXJJOI2018_T1_market
题目描述 某天Lemon去超市买柠檬,他发现货架上有N个柠檬,每个柠檬都有一个重量Wi和价格Ci. Lemon身上只带了S元钱,因此他想要买一个价格不超过S的柠檬回家,另外,他希望他买的那个柠檬的性价 ...