scrapy pipeline(scrapy怎样建立多个spider)

本文目录
- scrapy怎样建立多个spider
- scrapy-redis分布式爬虫启动为什么会等待
- scrapy可以一次性返回多个不同的item给pipeline处理吗
- scrapy多个spider怎么指定pipeline
- scrapy item pipeline 什么时候执行
- scrapy不进入pipeline的原因排查
scrapy怎样建立多个spider
1、scrapy默认支持多爬虫,爬虫名区分就可以了
2、item是数据库orm,新建class就行了,
3、pipeline管道,可以更加爬虫名入不同的数据库
4、setting设置数据库等配置信息
scrapy-redis分布式爬虫启动为什么会等待
scrapy-redis所实现的两种分布式:爬虫分布式以及item处理分布式。分别是由模块scheduler和模块pipelines实现。
一、Scrapy-redis各个组件介绍
(I) connection.py
负责根据setting中配置实例化redis连接。被dupefilter和scheduler调用,总之涉及到redis存取的都要使用到这个模块。
(II) dupefilter.py
负责执行requst的去重,实现的很有技巧性,使用redis的set数据结构。但是注意scheduler并不使用其中用于在这个模块中实现的dupefilter键做request的调度,而是使用queue.py模块中实现的queue。
当request不重复时,将其存入到queue中,调度时将其弹出。
(III)queue.py
其作用如II所述,但是这里实现了三种方式的queue:
FIFO的SpiderQueue,SpiderPriorityQueue,以及LIFI的SpiderStack。默认使用的是第二中,这也就是出现之前文章中所分析情况的原因(链接)。
(IV)pipelines.py
这是是用来实现分布式处理的作用。它将Item存储在redis中以实现分布式处理。
另外可以发现,同样是编写pipelines,在这里的编码实现不同于文章(链接:)中所分析的情况,由于在这里需要读取配置,所以就用到了from_crawler()函数。
(V)scheduler.py
此扩展是对scrapy中自带的scheduler的替代(在settings的SCHEDULER变量中指出),正是利用此扩展实现crawler的分布式调度。其利用的数据结构来自于queue中实现的数据结构。
scrapy-redis所实现的两种分布式:爬虫分布式以及item处理分布式就是由模块scheduler和模块pipelines实现。上述其它模块作为为二者辅助的功能模块。
(VI)spider.py
设计的这个spider从redis中读取要爬的url,然后执行爬取,若爬取过程中返回更多的url,那么继续进行直至所有的request完成。之后继续从redis中读取url,循环这个过程。
二、组件之间的关系
三、scrapy-redis实例分析
(1) spiders/ ebay_redis.py
classEbayCrawler(RedisMixin,CrawlSpider):
"""Spiderthat reads urls from redis queue (mycrawler:start_urls)."""
name = ’ebay_redis’
redis_key = ’ ebay_redis:start_urls’
rules = (
# follow all links
# Rule(SgmlLinkExtractor(),callback=’parse_page’, follow=True),
Rule(sle(allow=(’+/itm/’, )), callback=’parse_item’),
)
#该方法是最关键的方法,该方法名以下划线开头,建立了和redis的关系
def _set_crawler(self, crawler):
CrawlSpider._set_crawler(self, crawler)
RedisMixin.setup_redis(self)
# 解析sku页面
defparse_item(self,response):
sel =Selector(response)
base_url =get_base_url(response)
item = EbayphoneItem()
print base_url
item
item/text()").extract()
return item
该类继承了RedisMixin(scrapy_redis/spiders.py中的一个类)和CrawlSpider,加载配置文件的各项,建立和redis的关联,同时进行抓取后的解析。关键方法为_set_crawler(self, crawler),关键属性是redis_key,该key如果没有初始化则默认为spider.name:start_urls
_set_crawler()方法是如何被调用的:
scrapy/crawl.py/Crawler: crawl() -》
scrapy/crawl.py/Crawler:_create_spider () -》
CrawlSpider:from_crawler() –》
scrapy/spiders/Spider: from_crawler() -》
ebay_redis.py :_set_crawler()
(2) setting.py
SPIDER_MODULES=
NEWSPIDER_MODULE= ’example.spiders’
ITEM_PIPELINES = {
’example.pipelines.ExamplePipeline’:300,
#通过配置下面该项RedisPipeline’会将item写入key为
#spider.name:items的redis的list中,供后面的分布式处理item
’scrapy_redis.pipelines.RedisPipeline’:400,
}
SCHEDULER= "scrapy_redis.scheduler.Scheduler"
#不清理redisqueues, 允许暂停或重启crawls
SCHEDULER_PERSIST= True
SCHEDULER_QUEUE_CLASS= ’scrapy_redis.queue.SpiderPriorityQueue’
#该项仅对queueclass is SpiderQueue or SpiderStack生效,阻止spider被关闭的最大空闲时间
SCHEDULER_IDLE_BEFORE_CLOSE= 10
#连接redis使用
REDIS_HOST = ’123.56.184.53’
REDIS_PORT= 6379
(3) process_items.py:
defmain():
pool =redis.ConnectionPool(host=’123.56.184.53’, port=6379, db=0)
r = redis.Redis(connection_pool=pool)
while True:
# process queue as FIFO, change `blpop`to `brpop` to process as LIFO
source, data =r.blpop()
item = json.loads(data)
try:
print u"Processing: %(name)s《%(link)s》" % item
except KeyError:
print u"Error procesing:%r" % item
if__name__ == ’__main__’:
main()
该模块是从redis对应的list中取出item,进行处理,可以运行多个进程分布式处理items
(4)执行过程如下:
首先在redis服务器端打开redis服务:
./redis-server
其次执行
***隐藏网址***
然后运行爬虫:
scrapy runspiderebay_redis.py
可以执行多个爬虫,同时对ebay_redis:start_urls中的url进行分布式爬取,爬取后的结果都存入了ebay_redis:items的list中,供后续再次处理
最后可以查看items队列中的内容
./redis-cli llen ebay_redis:items 可以看到该items中总的个数
scrapy可以一次性返回多个不同的item给pipeline处理吗
在 items.py 建立不同的item 类
items.py
from scrapy import Item, Fieldclass Item1(Item):
passclass Item2(Item):
pass
custom_spider.pyfrom scray.spider import BaseSpiderfrom custom_spider.items import Item1, Item2class Spider(BaseSpider):
def __init__(self):
pass
def parse(self):
item_a = Item1()
item_b = Item2()
scrapy多个spider怎么指定pipeline
定义多个pipeline类,在settings里面打开想要打开的pipeline就行。也可以在custom_settings里面配置。
scrapy item pipeline 什么时候执行
pipeline 主要包含以下三个函数
1 def open_spider(self, spider)
这是爬虫开始时执行的,可以在这里初始化相关参数
2 def process_item(self, item, spider)
这个方法一般在爬虫程序中yield调用的时候被触发,其中一个参数item就是yield传过来的,可以在这里处理抓到的数据,比如说进一步格式化,数据保存等。
3 def close_spider(self, spider)
爬虫结束执行时触发,在这里可以进行文件关闭,数据库关闭等处理。
scrapy不进入pipeline的原因排查
1、首先排查pipeline类是否有在settings中注册
2、查看spider中最后有没有忘记写yield item
一般是由于第二个原因。
顺便提一句,scrapy运行起来之后,pipeline只有一个实例。
所以去重的话,如果是单机模式,可以在pipeline中采用set去重。

更多文章:
js代码炫酷特效模板(鼠标悬停特效代码怎么写,鼠标放在小图片上旁边显示一张大图片)
2026年5月14日 04:00
strutsprepareandexecutefilter(struts2原理)
2026年4月13日 00:30
物流管理系统数据库设计文档(求基于C#的库存管理系统的设计与实现毕业设计论文)
2025年12月2日 13:30
executed alternatively(各位帅哥 靓妹 英文好的 江湖救急了 谢谢!!)
2026年4月4日 05:15
arraylist和array区别(ArraryList,Array string[]的差别是什么)
2026年1月4日 01:00
reset sw不分方向(微星H55M-P32主板开启,重启,指示灯接口,应该如何插)
2026年1月27日 04:45
黑莓开发者模式(黑莓KEY2 Lite 开发者选项怎么打开)
2025年6月23日 08:15
in length的用法(新概念英语第2册Lesson90~92重点句型及语法)
2026年4月9日 10:45
webpack缺点(如何理解webpack文档中对AMD缺点的描述)
2026年9月10日 12:30
corresponding author和第一作者(corresponding author必须是第一作者吗)
2026年5月12日 05:30












