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) ...
随机推荐
- mongo rename collection
db.getCollection('a').renameCollection("b"); db.getCollection('a').find({}, {_id: 0}).forE ...
- asp.net---jquery--ajax 实现滚动条滚动到底部分页显示
前台:aspx页面 var bgtime = $(" #date1 ").val(); var overtime = $(" #date2 ").val(); ...
- Office 365 的安装方法
一.在线安装 进入网址 https://www.office.com/ 使用office账号登陆 1.点击右上角安装office应用,选择第二项 其他安装选项 2.选择安装语言 点击高级,选择安装版本 ...
- @Value默认值填null
@Value("${topology.position.spout.maxpending:#{null}}") private Integer spoutMaxPending; @ ...
- C 语言高效编程与代码优化
译文链接:http://www.codeceo.com/article/c-high-performance-coding.html英文原文:Writing Efficient C and C Cod ...
- Painter
时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 杂货店出售一种由N(3<=N<=12)种不同颜色的颜料,每种一瓶(50ML),组成的颜料套装. 你现在需要使用这 ...
- html和jsp页面中把文本框禁用,只能读不能写的方法
方法常用有三种: 第一种,使用 onfocus="this.blur()" <input name="deptno" type="text& ...
- jquery mobile AJAX特性的陷阱
简单情况是 MVC 重定向,URL不变 试了N种方式,跳来跳去,无解,服务端跳,写JS跳,生成跳转中间页跳.失败 后来一看,明明已经跳到新页了,样式什么还是原页的,有点火大了. 出去溜一圈,喝杯水,和 ...
- MySQL5.7彻底取消主从复制
由于手误在master节点执行了stop slave;->change master to XXX;->start slave;的动作,后面虽然使用stop slave停止了主从复制,但是 ...
- 一、HTTP和HTTPS的基本概念
1HTTP:是互联网上应用最为广泛的一种网络协议,是一个客户端和服务器端请求和应答的标准(TCP),用于从WWW服务器传输超文本到本地浏览器的传输协议,它可以使浏览器更加高效,使网络传输减少. 2HT ...