middlewares.py
1.数据需要selenium解析后才能呈现
# middlewares.py from scrapy import signals import scrapy from selenium import webdriver from selenium.webdriver.chrome.options import Options import time class ChromedriverMiddleware(object): def process_request(self, request, spider): chrome_options = Options() chrome_options.add_argument('--headless') # 使用无头谷歌浏览器模式 chrome_options.add_argument('--disable-gpu') chrome_options.add_argument('--no-sandbox') # 指定谷歌浏览器路径 self.driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='/root/zx/spider/driver/chromedriver') if request.url != 'https://www.aqistudy.cn/historydata/': self.driver.get(request.url) time.sleep(1) html = self.driver.page_source self.driver.quit() return scrapy.http.HtmlResponse(url=request.url, body=html.encode('utf-8'), encoding='utf-8', request=request) # setting.py DOWNLOADER_MIDDLEWARES = { 'driver.middlewares.ChromedriverMiddleware': 543, }
2.使用代理
# middlewares.py from scrapy.utils.python import to_bytes import base64 class ProxyMiddleware(object): def process_request(self, request, spider): # 全部请求都使用代理 PROXIES = [ {'ip_port': '111.11.228.75:80', 'user_pass': 'root@123456'}, {'ip_port': '120.198.243.22:80', 'user_pass': 'root@123456'}, {'ip_port': '111.8.60.9:8123', 'user_pass': 'root@123456'}, {'ip_port': '101.71.27.120:80', 'user_pass': 'root@123456'}, {'ip_port': '122.96.59.104:80', 'user_pass': 'root@123456'}, {'ip_port': '122.224.249.122:8088', 'user_pass': 'root@123456'}, ] proxy = random.choice(PROXIES) if proxy['user_pass'] is not None: request.meta['proxy'] = to_bytes("http://%s" % proxy['ip_port']) encoded_user_pass = base64.b64encode(to_bytes(proxy['user_pass'])) request.headers['Proxy-Authorization'] = to_bytes('Basic ' + encoded_user_pass) else: request.meta['proxy'] = to_bytes("http://%s" % proxy['ip_port']) def process_exception(self, request, response, spider): # 请求报错是执行代理 def process_request(self, request, spider): PROXIES = [ {'ip_port': '111.11.228.75:80', 'user_pass': 'root@123456'}, {'ip_port': '120.198.243.22:80', 'user_pass': 'root@123456'}, {'ip_port': '111.8.60.9:8123', 'user_pass': 'root@123456'}, {'ip_port': '101.71.27.120:80', 'user_pass': 'root@123456'}, {'ip_port': '122.96.59.104:80', 'user_pass': 'root@123456'}, {'ip_port': '122.224.249.122:8088', 'user_pass': 'root@123456'}, ] proxy = random.choice(PROXIES) if proxy['user_pass'] is not None: request.meta['proxy'] = to_bytes("http://%s" % proxy['ip_port']) encoded_user_pass = base64.b64encode(to_bytes(proxy['user_pass'])) request.headers['Proxy-Authorization'] = to_bytes('Basic ' + encoded_user_pass) else: request.meta['proxy'] = to_bytes("http://%s" % proxy['ip_port']) # setting.py DOWNLOADER_MIDDLEWARES = { 'driver.middlewares.ProxyMiddleware': 543, # 设置不参与scrapy的自动重试的动作 请求出错时才使用代理 'scrapy.downloadermiddlewares.retry.RetryMiddleware': None } # 禁止重试 RETRY_ENABLED = False # 下载超时 DOWNLOAD_TIMEOUT = 10 # 下载重试次数 RETRY_TIMES=5 # spider.py 在请求中使用代理 yield scrapy.FormRequest(self.start_url, method="POST", formdata=self.form_data,meta={'proxy':"http://59.44.247.194:9797"}) # proxy_pool 设置代理 class MyproxiesSpiderMiddleware(object): def process_request(self, request, spider): proxies = requests.get('http://127.0.0.1:5000/get').content.decode('utf-8') print(proxies) if 'pool' in proxies: time.sleep(80) print('没有可用的的代理,等60秒') proxies = requests.get('http://127.0.0.1:5000/get').content.decode('utf-8') request.meta["proxy"] = "http://{}".format(proxies) else: request.meta["proxy"] = "http://{}".format(proxies) # request.meta["proxy"] = "http://36.249.49.43:9999"
middlewares.py的更多相关文章
- Scrapy框架: middlewares.py设置
# -*- coding: utf-8 -*- # Define here the models for your spider middleware # # See documentation in ...
- 爬虫框架Scrapy之Downloader Middlewares
反反爬虫相关机制 Some websites implement certain measures to prevent bots from crawling them, with varying d ...
- Scrapy爬虫框架(2)--内置py文件
Scrapy概念图 这里有很多py文件,分别与Scrapy的各个模块对应 superspider是一个爬虫项目 spider1.py则是一个创建好的爬虫文件,爬取资源返回url和数据 items.py ...
- Python爬虫Scrapy框架入门(2)
本文是跟着大神博客,尝试从网站上爬一堆东西,一堆你懂得的东西 附上原创链接: http://www.cnblogs.com/qiyeboy/p/5428240.html 基本思路是,查看网页元素,填写 ...
- scrapy爬虫框架入门实例(一)
流程分析 抓取内容(百度贴吧:网络爬虫吧) 页面: http://tieba.baidu.com/f?kw=%E7%BD%91%E7%BB%9C%E7%88%AC%E8%99%AB&ie=ut ...
- scrapy 代理
说明: 本文参照了官网文档,以及stackoverflow的几个问题 概要: 在scrapy中使用代理,有两种使用方式 使用中间件 直接设置Request类的meta参数 方式一:使用中间件 要进行下 ...
- 如何让你的scrapy爬虫不再被ban
前面用scrapy编写爬虫抓取了自己博客的内容并保存成json格式的数据(scrapy爬虫成长日记之创建工程-抽取数据-保存为json格式的数据)和写入数据库(scrapy爬虫成长日记之将抓取内容写入 ...
- python爬虫之Scrapy 使用代理配置
转载自:http://www.python_tab.com/html/2014/pythonweb_0326/724.html 在爬取网站内容的时候,最常遇到的问题是:网站对IP有限制,会有防抓取功能 ...
- Win10环境下的Scrapy结合Tor进行匿名爬取
本文内容来源:http://blog.privatenode.in/torifying-scrapy-project-on-ubuntu/ 在使用Scrapy的时候,一旦进行高频率的爬取就容易被封IP ...
随机推荐
- (入门SpringBoot)SpringBoot加接口操作日志好方法(九)
用Spring的切面去做,慕课网上的大神的小妙招,被我拷贝下来了.import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotati ...
- java8接口
// 可以用来做工具类// 这个注解是函数式注解,表示这个接口里面有且仅有一个抽象方法, 默认方法可以有0个或多个@FunctionalInterfacepublic interface Interf ...
- mysql使用——sql实现随机取一条数据
最近在做接口测试的时候,测试数据是从数据库查询的,但是当需要并发多次去调用接口时,如果sql只是单纯的进行了limit取值,那并发的时候肯定会每条数据都一样. 因此,研究了下sql随机取一条数据的写法 ...
- Java Web 学习(2) —— JSP
JSP 一. 什么是 JSP JSP 和 Servlet Servlet 有两个缺点是无法克服的:首先,写在 Servlet 中的所有 HTML 标签必须包含 Java 字符串,这使得处理HTTP响应 ...
- maven clean插件使用进阶
maven clean插件使用进阶 参考 Maven clean 插件 Maven删除外部文件 查看命令帮助 mvn clean:help mvn clean:help -Ddetail=true - ...
- spring注解实现事务
码云: https://gitee.com/MarkPolaris/spring-transcation
- Java连载45-继承举例、方法覆盖
一.Java语言中假设一个类没有显式的继承任何类,那么该类默认继承Java SE库中提供的java.lang.Object类 1.快捷键:Ctrl + shift + T:可以在Myeclipse中查 ...
- MySQL索引知识学习笔记
目录 一.索引的概念 二.索引分类 三.索引用法 四 .索引架构简介 五.索引适用的情况 六.索引不适用的情况 继我的上篇博客:Oracle索引知识学习笔记,再记录一篇MySQL的索引知识学习笔记,本 ...
- visdom 简单使用
 官方网址: https://github.com/facebookresearch/visdom 入门教程: http://www.ainoobtech.com/pytorch/pytorch-v ...
- go语言中map每次遍历的顺序不同-问题分析
WHAT? 发现下面这段代码,多次运行出的结果是不一样的 mapper := make(map[int]string) mapper[1] = "1" mapper[2] = &q ...