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的更多相关文章

  1. scrapy中间件中发送邮件

    背景介绍:之前写过通过通过scrapy的扩展发送邮件,在爬虫关闭的时候发送邮件.那个时候有个问题就是MailSender对象需要return出去.这次需要在中间件中发送邮件,但是中间件中不能随便使用r ...

  2. Scrapy中间件user-agent和ip代理使用

    一.定义实现随机User-Agent的下载中间件 1.在middlewares.py中完善代码 import random from Tencent.settings import USER_AGEN ...

  3. 在Scrapy中使用selenium

    在scrapy中使用selenium 在scrapy中需要获取动态加载的数据的时候,可以在下载中间件中使用selenium 编码步骤: 在爬虫文件中导入webdrvier类 在爬虫文件的爬虫类的构造方 ...

  4. scrapy——中间件UserAgent代理

    pip install fake-useragent 使用说明:from fake_useragent import UserAgent# 实例化一个UserAgent对象ua = UserAgent ...

  5. Scrapy中集成selenium

    面对众多动态网站比如说淘宝等,一般情况下用selenium最好 那么如何集成selenium到scrapy中呢? 因为每一次request的请求都要经过中间件,所以写在中间件中最为合适 from se ...

  6. 第三百五十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—将selenium操作谷歌浏览器集成到scrapy中

    第三百五十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—将selenium操作谷歌浏览器集成到scrapy中 1.爬虫文件 dispatcher.connect()信号分发器,第一个参数信 ...

  7. scrapy中的selenium

    引入 在通过scrapy框架进行某些网站数据爬取的时候,往往会碰到页面动态数据加载的情况发生,如果直接使用scrapy对其url发请求,是绝对获取不到那部分动态加载出来的数据值.但是通过观察我们会发现 ...

  8. scrapy中使用selenium来爬取页面

    scrapy中使用selenium来爬取页面 from selenium import webdriver from scrapy.http.response.html import HtmlResp ...

  9. 如何优雅的在scrapy中使用selenium —— 在scrapy中实现浏览器池

    1 使用 scrapy 做采集实在是爽,但是遇到网站反爬措施做的比较好的就让人头大了.除了硬着头皮上以外,还可以使用爬虫利器 selenium,selenium 因其良好的模拟能力成为爬虫爱(cai) ...

随机推荐

  1. 4)date中的Ymd格式问题

    以下是详细的参数: format 字符 说明 返回值例子日 --- ---d 月份中的第几天,有前导零的 2 位数字 01 到 31D 星期中的第几天,文本表示,3 个字母 Mon 到 Sunj 月份 ...

  2. 非参数检验|Sign test|Wilcoxon signed rank test|Wilcoxon rank sum test|Bootstrapping

    非参数检验条件没有参数,因此就没有分布,利用数据等级之间的差距,依次赋值之后再用参数方法测试.将连续型变量转化为离散型变量,即顺序变量.与参数检验相比,正态分布较弱(p值有可能不显著,浪费信息,比如最 ...

  3. python学习笔记(3)数据类型-列表list

    序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见的是列表和元组. 序列 ...

  4. cs231n spring 2017 lecture9 CNN Architectures

    参考<deeplearning.ai 卷积神经网络 Week 2 听课笔记>. 1. AlexNet(Krizhevsky et al. 2012),8层网络. 学会计算每一层的输出的sh ...

  5. Esp8266和HomeKit

    Summary 没有找到合适的简单解决方案,将Esp8266控制的设备连接到HomeKit.所以参照EspEasy实现 HomeKit和Esp8266连接. 连接方式: Raspberry Zero ...

  6. 千万不要在module里扩展较多逻辑,很容易引起项目异常。

    NOP项目 为保持紧跟NOP更新,项目组坚持不改NOP源码. 以触发器,插件化开发为拓展模式 NOP自定义好的接口或完全独立的新拓展功能很容易插件化. 但部分功能要在NOP原项目上扩展修改在不改源码的 ...

  7. Android编程权威指南第三版 第32章

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_35564145/article/de ...

  8. AI超越人类大脑,或许是场“别有用心者”的骗局

    ​ 谷歌.微软.苹果.特斯拉.百度.腾讯.阿里等互联网巨头企业,以及纳德拉.马斯克.扎克伯格.马云等互联网大佬,近年来一直都对人工智能--AI非常上心.在众多场合对AI给予了或肯定,或恐惧的评价.但无 ...

  9. HINOC2.0标准介绍(1):概述

    本文首发于'瀚诺观察'微信公众号 摘要: 2016年3月18日,国家新闻出版广电总局批准发布了行业标准GY/T 297-2016<NGB宽带接入系统HINOC2.0物理层和媒体接入控制层技术规范 ...

  10. swap和shm的区别

    在使用docker的过程中,发现其有很多内存相关的命令,对其中的swap(交换内存)和shm(共享内存)尤其费解.于是查阅了一些资料,弄明白了二者的基本区别. swap 是一个文件,是使用硬盘空间的一 ...