scrapy之Pipeline
官方文档:https://docs.scrapy.org/en/latest/topics/item-pipeline.html
激活pipeline,需要在settings里配置,然而这里配置的pipeline会作用于所有的spider。加入项目中有很多spider在运行。item pipeline的处理就会很麻烦,你可以通过process_item(self,item,spider)中的spider参数来判断是来自哪个爬虫,但是这种方法很冗余。更好的做法是配置spider类中的custom_settings属性。为每一个spider配置不同的pipeline。示例如下:
同时,这里你也会看到custom_settings的用法和用处。
class XiaohuaSpider(scrapy.Spider):
name = 'xiaohua'
custom_settings = {
'ITEM_PIPELINES ':{
'TB.pipelines.TBMongoPipeline':300,
}
}
一 method
1 process_item(self,item,spider)
This method is called for every item pipeline component
2 open_spider(self,spider)
This method is called when the spider is opened.
3 close_spider(self,spider)
4 from_crawler(cls,crawler)
It must return a new instance of the pipeline
二 Item Pipeline example
1 write items to mongodb
import pymongo
class MongoPipeline(object):
collection_name = 'scrapy_items'
def __init__(self, mongo_uri, mongo_db):
self.mongo_uri = mongo_uri
self.mongo_db = mongo_db
@classmethod
def from_crawler(cls, crawler):
return cls(
mongo_uri=crawler.settings.get('MONGO_URI'),
mongo_db=crawler.settings.get('MONGO_DATABASE', 'items')
)
def open_spider(self, spider):
self.client = pymongo.MongoClient(self.mongo_uri)
self.db = self.client[self.mongo_db]
def close_spider(self, spider):
self.client.close()
def process_item(self, item, spider):
self.db[self.collection_name].insert_one(dict(item))
return item
2 duplicates filter
from scrapy.exceptions import DropItem
class DuplicatesPipeline(object):
def __init__(self):
self.ids_seen = set()
def process_item(self, item, spider):
if item['id'] in self.ids_seen:
raise DropItem("Duplicate item found: %s" % item)
else:
self.ids_seen.add(item['id'])
return item
scrapy之Pipeline的更多相关文章
- python爬虫之scrapy的pipeline的使用
scrapy的pipeline是一个非常重要的模块,主要作用是将return的items写入到数据库.文件等持久化模块,下面我们就简单的了解一下pipelines的用法. 案例一: items池 cl ...
- scrapy中pipeline的一点综合知识
初次学习scrapy ,觉得spider代码才是最重要的,越往后学,发现pipeline中的代码也很有趣, 今天顺便把pipeline中三种储存方法写下来,算是对自己学习的一点鼓励吧,也可以为后来者的 ...
- scrapy item pipeline
item pipeline process_item(self, item, spider) #这个是所有pipeline都必须要有的方法在这个方法下再继续编辑具体怎么处理 另可以添加别的方法 ope ...
- 爬虫(十三):scrapy中pipeline的用法
当Item 在Spider中被收集之后,就会被传递到Item Pipeline中进行处理 每个item pipeline组件是实现了简单的方法的python类,负责接收到item并通过它执行一些行为, ...
- Python爬虫知识点四--scrapy框架
一.scrapy结构数据 解释: 1.名词解析: o 引擎(Scrapy Engine)o 调度器(Scheduler)o 下载器(Downloader)o 蜘蛛(Spiders)o 项目管 ...
- scrapy 博客爬取
item.py import scrapy class FulongpjtItem(scrapy.Item): # define the fields for your item here like: ...
- scrapy爬虫学习系列五:图片的抓取和下载
系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备: http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...
- 爬虫系列----scrapy爬取网页初始
一 基本流程 创建工程,工程名称为(cmd):firstblood: scrapy startproject firstblood 进入工程目录中(cmd):cd :./firstblood 创建爬虫 ...
- web全栈应用【爬取(scrapy)数据 -> 通过restful接口存入数据库 -> websocket推送展示到前台】
作为 https://github.com/fanqingsong/web_full_stack_application 子项目的一功能的核心部分,使用scrapy抓取数据,解析完的数据,使用 pyt ...
随机推荐
- linux文本处理工具及正则表达式
cat命令:查看文本内容 cat [选项]... [文件]... -E 显示行结束符 -n 显示文本内容时显示行号 -A 显示所以控制符 -b 非空行编号 -s 压缩连 ...
- UITableView上添加按钮,按钮点击效果延迟的解决办法
在自定义的TableView的初始化方法做如下操作 - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame: ...
- SVN:The working copy is locked due to a previous error (二)
之前也碰到过这种问题,但是根本问题不同,解决方案不同. 传送门:SVN:The working copy is locked due to a previous error (二) 本次错误如图: 解 ...
- iOS监听电话来电、挂断、拨号等
以下,来讲解在app内如何调用打电话功能和监听电话来电.挂断.拨号等功能. 简单的UI布局: 首先,先实现拨打电话的功能,以便于后续测试: // 拨打电话 - (IBAction)dialingBut ...
- 如何挂载一个镜像文件(how to mount an image file)
如何挂载一个镜像文件(how to mount an image file) 08/16/2012master 4 Comments 在使用KVM或Xen虚拟化的情况下,经常需要使用镜像文件(imag ...
- nodejs 静态资源服务与接口代理跨域
首先需要 npm install express 和 npm install request 代码如下: const express = require('express'); const path ...
- PAT 乙级 1086
题目 题目地址:PAT 乙级 1086 思路 本题比较简单,但还是存在小小的坑点,简单说一下: 倒置中需要注意的唯一问题就是:100倒置后不是001,而是1:这个问题处理之后还要注意另一个点就是,10 ...
- Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery)
Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery) 体验 冒号搜索 1. 获取谷歌搜索api 谷歌搜索api教程 2. 后台调用 程序入口 main.go // ...
- Linux系统修改网卡名(eth0-3)
一.命名规则策略 规则1: 对于板载设备命名合并固件或 BIOS 提供的索引号,如果来自固件或 BIOS 的信息可读就命名,比如eno1,这种命名是比较常见的,否则使用规则2. 规则2: 命名合并固件 ...
- 为什么要用 ORM? 和 JDBC 有何不一样?
orm是一种思想,就是把object转变成数据库中的记录,或者把数据库中的记录转变objecdt,我们可以用jdbc来实现这种思想,其实,如果我们的项目是严格按照oop方式编写的话,我们的jdbc程序 ...