源码

class HttpErrorMiddleware(object):

    @classmethod
def from_crawler(cls, crawler):
return cls(crawler.settings) def __init__(self, settings):
self.handle_httpstatus_all = settings.getbool('HTTPERROR_ALLOW_ALL')
self.handle_httpstatus_list = settings.getlist('HTTPERROR_ALLOWED_CODES') def process_spider_input(self, response, spider):
if 200 <= response.status < 300: # common case
return
meta = response.meta
if 'handle_httpstatus_all' in meta:
return
if 'handle_httpstatus_list' in meta:
allowed_statuses = meta['handle_httpstatus_list']
elif self.handle_httpstatus_all:
return
else:
allowed_statuses = getattr(spider, 'handle_httpstatus_list', self.handle_httpstatus_list)
if response.status in allowed_statuses:
return
raise HttpError(response, 'Ignoring non-200 response') def process_spider_exception(self, response, exception, spider):
if isinstance(exception, HttpError):
spider.crawler.stats.inc_value('httperror/response_ignored_count')
spider.crawler.stats.inc_value(
'httperror/response_ignored_status_count/%s' % response.status
)
logger.info(
"Ignoring response %(response)r: HTTP status code is not handled or not allowed",
{'response': response}, extra={'spider': spider},
)
return []

  

通过源码 init函数可以看到可以配置两个配置

HTTPERROR_ALLOW_ALL = true
HTTPERROR_ALLOWED_CODES=[301,404]

第一个配置是否允许所有,就是收到响应后,不管什么状态码都返回给爬虫
第二个是允许的列表
以上是全局配置 不推荐 如果想在每个爬虫里面进行配置
可以在单独的爬虫里面设置
handle_httpstatus_all = true
handle_httpstatus_list = [404,302]
除非你非常熟悉你的网站和scrapy 不建议使用这些配置 ,因为把错误的响应也返回给爬虫,没什么用

scrapy 爬虫中间件 httperror中间件的更多相关文章

  1. scrapy爬虫-代理IP中间件

    class ProxyDownloaderMiddleware(object): # Not all methods need to be defined. If a method is not de ...

  2. python 全栈开发,Day138(scrapy框架的下载中间件,settings配置)

    昨日内容拾遗 打开昨天写的DianShang项目,查看items.py class AmazonItem(scrapy.Item): name = scrapy.Field() # 商品名 price ...

  3. scrapy框架4——下载中间件的使用

    一.下载中间件 下载中间件是scrapy提供用于用于在爬虫过程中可修改Request和Response,用于扩展scrapy的功能:比如: 可以在请求被Download之前,请求头部加上某些信息(例如 ...

  4. scrapy框架之下载中间件

    介绍 中间件是Scrapy里面的一个核心概念.使用中间件可以在爬虫的请求发起之前或者请求返回之后对数据进行定制化修改,从而开发出适应不同情况的爬虫. “中间件”这个中文名字和前面章节讲到的“中间人”只 ...

  5. Scrapy爬虫框架(实战篇)【Scrapy框架对接Splash抓取javaScript动态渲染页面】

    (1).前言 动态页面:HTML文档中的部分是由客户端运行JS脚本生成的,即服务器生成部分HTML文档内容,其余的再由客户端生成 静态页面:整个HTML文档是在服务器端生成的,即服务器生成好了,再发送 ...

  6. scrapy爬虫学习系列二:scrapy简单爬虫样例学习

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  7. scrapy爬虫框架介绍

    一 介绍 Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前Scrapy的用途十分广泛,可 ...

  8. Scrapy 爬虫

    Scrapy 爬虫 使用指南 完全教程   scrapy note command 全局命令: startproject :在 project_name 文件夹下创建一个名为 project_name ...

  9. scrapy爬虫框架setting模块解析

    平时写爬虫的时候并不需要设置setting里所有的参数,今天心血来潮,花了点时间查了一下setting模块创建后自动写入的所有参数的含义,记录一下. 模块相关说明信息 # -*- coding: ut ...

随机推荐

  1. SpringBoot 上下文获取注入的Bean

    import org.springframework.beans.BeansException; import org.springframework.context.ApplicationConte ...

  2. Ubuntu 16.04 Roboware安装和使用

    博客参考:https://blog.csdn.net/qq_41450811/article/details/80305846 RoboWare Studio是一个ROS集成开发环境.它使 ROS开发 ...

  3. Python 使用 paho-mqtt

    https://blog.csdn.net/weixin_41656968/article/details/80848542 https://blog.csdn.net/lhh08hasee/arti ...

  4. 百度AI文本审核API使用说明

    虽然,虽然,虽然,今天: 百度发布了2019年第一季度未经审计的财务报告.本季度百度营收241亿元人民币(约合35.9亿美元),同比增长15%,移除业务拆分收入影响,同比增长21%.低于市场预期242 ...

  5. (CSDN迁移)JAVA多线程实现-继承Thread

    继承Thread方法: extends Thread 重写覆盖run()方法: @Override public void run() 通过start()方法启动线程. threadDemo01.st ...

  6. 怎么在eclipse中安装properties插件

    原文地址:https://jingyan.baidu.com/article/380abd0a6abe731d90192ce4.html 首先,在eclipse中点击“help”-"Inst ...

  7. lvm的一些特殊命令

    pvscan --cache # 将lvm信息同步到其他节点 ......未完待续

  8. Debian系Linux源码安装Redis5.0.6

    一,先在官网下载源码包:https://redis.io/download 二,解压源码包,并cd到解压后的目录: 三,执行make MALLOC=libc: 接着cd src[解压的目录里有这个子目 ...

  9. 在CAD中插入谷歌地球卫星地图

    本文主要介绍如何在CAD中插入谷歌地球卫星地图,作为参照光栅图像.谷歌地球卫星地图使用“迈高图-地图数据下载器”(以下简称:迈高图)下载.迈高图会给出相关插入参数(插入点和缩放比例),保证插入卫星地图 ...

  10. django+uWSGI+nginx的工作原理流程与部署过程

    django+uWSGI+nginx的工作原理流程与部署过程 一.前言 知识的分享,不应该只是展示出来,还应该解释这样做是为什么... 献给和我一样懵懂中不断汲取知识,进步的人们. 授人与鱼,不如授人 ...