
class NewrecordSpiderMiddleware(object):
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the spider middleware does not modify the
# passed objects.
@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s
# 进入spider 的response 的数据
# 和 downloadmiddle里面的process_response类似
# 先去downloadmiddleware那里的process_response 再到这里处理:
def process_spider_input(self, response, spider): # 处理进入spider中的response数据,但返回的是None
print('-----------------------3--------------------') # 对response筛选之后不能阻止进入spider 啥用?try--except---Exception,
print('---进入spidermiddleware----process_spider_input------response.url----%s--------'%(response.url))
# Called for each response that goes through the spider
try:
# middleware and into the spider.
# Should return None or raise an exception.
return None
except Exception as e:
print(e)
def process_spider_output(self, response, result, spider):
# Called with the results returned from the Spider, after
# it has processed the response.
# Must return an iterable of Request, dict or Item objects.
# result :经过parse_item 处理过后的输出结果,等于item数据也可以在这里处理,不过是在Pipline处理过后的数据
# parse_item 输出的结果先进入pipeline管道里去处理item数据最后回到process_spider_output这里,再就是关闭spider:
for i in result:
yield i
def process_spider_exception(self, response, exception, spider):
# Called when a spider or process_spider_input() method
# (from other spider middleware) raises an exception.
# Should return either None or an iterable of Response, dict
# or Item objects.
pass
# 处理start_urls 后面的url无关: 否则这方法不会运行,只能是start_urls参数
# 并且def 里面的东西只能是process_start_requests
# 处理start_urls 与后面的url无关:
def process_start_requests(self, start_urls, spider):
# Called with the start requests of the spider, and works # similarly to the process_spider_output() method, except
# that it doesn’t have a response associated.
# Must return only start_urls (not items).
for r in start_urls:
if str(r).find('rank_news') >= 0:
print('---------------------0-----------------------------')
print('-------------------进入Spider MiddleWare里面的开始爬去网页url-----------start_requests===:%s', r)
yield r
def spider_opened(self, spider):
spider.logger.info('Spider opened: %s ' %spider.name)
class NewrecordDownloaderMiddleware(object):
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the downloader middleware does not modify the
# passed objects.
@classmethod
def from_crawler(cls, crawler):
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s
# Proxy-Authorization base64代理账户验证
# encoded_user_pass = base64.b64encode(proxy_user_pass)
# request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass
# request.meta['proxy'] = ['127.0.0.1:8000']
# request.meta['item']='' 在request meta 数据里面增加数据 可以用来传参
# request(url, meta['item']=item[], callback= '')
# request.cookies['']='' 往request里面增加cookies
def process_request(self, request, spider):
print('---------------1------------------')
print('----------------进入DownloadMiddleWare中的request的url是:%s----------------' %(request.url))
return None
# return None: continue processing this exception
# return a Response object: stops process_exception() chain
# return a Request object: stops process_exception() chain
def process_response(self, request, response, spider): # 处理所有爬过的网站的response,通过response.url 可以筛选
print('-----------------------------2---------------------------------') # 需要的爬取的网址,但这个在Rules里面更方便
print('----------------进入DownloadMiddleWare中的response的url是:%s----------------' %(response.url))
return response # 返回的response 进入spider 中的process_spider_input
def process_exception(self, request, exception, spider):
pass
def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name)
121 1,19 顶端
- Scrapy框架学习(三)Spider、Downloader Middleware、Spider Middleware、Item Pipeline的用法
Spider有以下属性: Spider属性 name 爬虫名称,定义Spider名字的字符串,必须是唯一的.常见的命名方法是以爬取网站的域名来命名,比如爬取baidu.com,那就将Spider的名字 ...
- 小白学 Python 爬虫(37):爬虫框架 Scrapy 入门基础(五) Spider Middleware
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 爬虫(十六):Scrapy框架(三) Spider Middleware、Item Pipeline
1. Spider Middleware Spider Middleware是介入到Scrapy的Spider处理机制的钩子框架. 当Downloader生成Response之后,Response会被 ...
- python爬虫scrapy之downloader_middleware设置proxy代理
一.背景: 小编在爬虫的时候肯定会遇到被封杀的情况,昨天爬了一个网站,刚开始是可以了,在settings的设置DEFAULT_REQUEST_HEADERS伪装自己是chrome浏览器,刚开始是可以的 ...
- scrapy代理的设置
scrapy代理的设置 在我的上一篇文章介绍了scrapy下载器中间件的使用,这里的scrapyIP的代理就是用这个原理实现的,重写了下载器中间件的process_request(self,reque ...
- scrapy.Spider的属性和方法
scrapy.Spider的属性和方法 属性: name:spider的名称,要求唯一 allowed_domains:允许的域名,限制爬虫的范围 start_urls:初始urls custom_s ...
- scrapy spider官方文档
Spiders Spider类定义了如何爬取某个(或某些)网站.包括了爬取的动作(例如:是否跟进链接)以及如何从网页的内容中提取结构化数据(爬取item). 换句话说,Spider就是您定义爬取的动作 ...
- scrapy spider
spider 定义:在spiders文件夹中由用户自定义,继承scrapy.Spider类或其子类 Spider并没有提供什么特殊的功能. 其仅仅请求给定的 start_urls/start_requ ...
- scrapy的allowed_domains设置含义
设置allowed_domains的含义是过滤爬取的域名,在插件OffsiteMiddleware启用的情况下(默认是启用的),不在此允许范围内的域名就会被过滤,而不会进行爬取 但是有一个问题:像下面 ...
随机推荐
- JAVA SpringBoot2 关于 JSON 数据处理,基于 ObjectMapper
1,当今的互联网开发行业,JSON 这种数据格式越来越成为网络开发的主流,尤其是前后端分离之后,几乎百分百的数据交互方式都是采用 JSON 2,由于 SpringMVC 框架的封装性,我们日常开发中只 ...
- jedisCluster 报错: redis.clients.jedis.exceptions.JedisClusterException: No way to dispatch this command to Redis Cluster because keys have different slots.
根本原因:jedisCluster不支持mget/mset等跨槽位的操作. 版本:2.9.0 解决办法,推荐更改redis的驱动修改为: lettuce lettuce 项目地址:https://gi ...
- CSS设计中的错误大整理!
如果有人发明时间机器,那应该将这些错误纠正,不然可把前端程序猿们给还惨了.大家一起看看都有哪些CSS规则应该完善. (CSS 代码) white-space: nowrap 应该 white-spac ...
- vue Element学习和问题处理
1. resetForm内容没有完全被重置在使用resetForm时,会还原数据到初始化data时的值,有时会出现值已修改,但页面无刷新变化.添加: this.$nextTick(() => { ...
- 回溯法 Generate Parentheses,第二次总结
class Solution { public: vector<string> ans; void helper(string& cur, int left, int right, ...
- xfs 的一些工具使用
[root@localhost caq]# xfs_db -c frag -r /dev/sdaw actual , ideal , fragmentation factor 82.56% Note, ...
- getBoundingClientRect获取元素在页面上的位置
getBoundingClientRect用于获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置. getBoundingClientRect是DOM元素到浏览器可视范围的距离(不包含文档卷起 ...
- jQuery之位置坐标图形相关方法
jQuery实例方法-位置图形 位置坐标图形大小相关方法: .offset() .position() .scrollTop() ..scrollLeft() .width()..height() . ...
- OpenCV4.0学习笔记
1.读取显示图像 #include<opencv2/opencv.hpp> #include<iostream> using namespace cv; using names ...
- find 递归/不递归 查找子目录的方法
1.递归查找(find 命令 是递归遍历文件夹的) 命令:find . -name “*.txt” //当前路径下递归查找以.txt结尾的文件夹 2.不递归查找 find . -name “*.txt ...