Scrapy学习笔记VII--Item Pipeline
来源:互联网 发布:wp服务器软件 编辑:程序博客网 时间:2024/06/15 05:33
通过spider爬取到所需的数据存放在item后,item可以发送到item pipeline 进行进一步的处理(通过几个组件-python class),如是否需要pipeline进行进一步处理,或者丢弃,或者不再处理它。
item pipeline 使用场景:
- 清除HTML数据
- 验证爬取的数据(检查是否包含某一字段)
- 检查是否有重复数据(duplicate),然后去重
- 把爬取的数据(scraped item)存入数据库(database)中
Writing your own item pipeline:
item pipeline 组件是一些Python类,必需实现以下几个方法:
process_item(self, item, spider) #对item中的数据进行处理,返回data字典,item对象,或者抛出其他异常,丢弃重复数据等
open_spider(self, spider) #当spider开始工作时,可以调用,如打开文件,打开数据库等等。。。,否则报错
close_spider(self, spider) #当关闭spider时调用,如关闭文件,关闭数据库等等。。。
from_crawler(cls, crawler) # 如果存在的话,从crawler创建一个pipeline实例。它必须返回一个新的pipeline实例。Crawler对象提供scrapy所有组件的接口,像setting,signals。item pipeline 可以使用它们而且可以把item pipeline实现这些功能写入scrapy中
Item pipeline example:
Price validation and dropping items with no prices
#Price validation and dropping items with no pricesfrom scrapy.exceptions import DropItem #抛出异常class PricePipeline(object): vat_factor = 1.15 def process_item(self, item, spider): if item['price']: if item['price_excludes_vat']: item['price'] = item['price'] * self.vat_factor return item else: raise DropItem("Missing price in %s" % item) #当没有价格数据时,抛出异常
Write items to a JSON file
把item写入json文件中,使用Feed exports,以下只是告诉如何写进pipeline中
import jsonclass JsonWriterPipeline(object): def open_spider(self, spider): self.file = open('items.jl', 'wb') def close_spider(self, spider): self.file.close() def process_item(self, item, spider): line = json.dumps(dict(item)) + "\n" #把item 转换为json格式 self.file.write(line) #写入文件 return item
Write items to MongoDB
写进mongodb数据库中,通过使用pymongo
import pymongoclass 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前加上@classmethod,可以通过类名去调用,也必须传递一个参数,一般cls 表示class,表示可以通过类直接调用 def from_crawler(cls, crawler): return cls( mongo_uri=crawler.settings.get('MONGO_URI'),#setting中设置address mongo_db=crawler.settings.get('MONGO_DATABASE', 'items') # 设置mongodb数据库名字 ) def open_spider(self, spider): #打开mongodb self.client = pymongo.MongoClient(self.mongo_uri) self.db = self.client[self.mongo_db] def close_spider(self, spider): #关闭mongodb self.client.close() def process_item(self, item, spider): self.db[self.collection_name].insert(dict(item)) return item #插入数据
Take screenshot of item
Duplicates filter
重复数据过滤,过滤已经处理过的数据
from scrapy.exceptions import DropItemclass 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 #未出现,添加到集合中
Activating an Item Pipeline component
使你添加的item pipeline生效,需要把定义的pipeline 类添加到ITEM_PIPELINES setting中
ITEM_PIPELINES = { 'myproject.pipelines.PricePipeline': 300, 'myproject.pipelines.JsonWriterPipeline': 800,} #数字决定了运行的顺序,0-1000,从低到高
0 0
- Scrapy学习笔记VII--Item Pipeline
- Scrapy笔记(6)- Item Pipeline
- scrapy 的 item pipeline
- scrapy爬虫之Item Pipeline
- 爬虫Scrapy-04Item Pipeline
- Scrapy框架学习(二)----Item Pipeline(管道)和Scrapy Shell
- 爬虫框架Scrapy之Item Pipeline
- Scrapy-Item Pipeline(项目管道)
- Scrapy学习笔记VI--Item Loaders
- Scrapy学习笔记(5)---Item Loader
- Scrapy源码分析-Item Pipeline中文文档(四)
- Scrapy爬虫入门教程九 Item Pipeline(项目管道)
- 探讨scrapy当中的pipeline何时获取item。
- Item Pipeline
- scrapy学习笔记--scrapy命令
- Scrapy 入门笔记(4) --- 使用 Pipeline 保存数据
- Spark ML PipeLine学习笔记
- Tensorflow 学习笔记:Input Pipeline
- JS中对象属性的可枚举性
- 微信小程序更新6大新功能,扩大使用场景
- 开发板刷系统的补充 4月11
- FFmpeg图解:结构体关系链接图
- 数据库设计规范
- Scrapy学习笔记VII--Item Pipeline
- React Native-2.CSS布局属性之伸缩项目属性
- JAVA经典面试题(一)-- 判断以及防止SQL注入
- 64位win10 python3.5环境安装scrapy
- CodeForces
- 多线程虚假唤醒
- React Native-3.React Native中的主要布局属性介绍和练习
- 安防大数据需要深度智能
- 关于Struts2简单的登陆页面遇到的错误error Exception occurred during processing request: null