笔记-scrapy-pipeline
笔记-scrapy-pipeline
1.简介
scrapy抓取数据后,使用yield发送item对象至pipeline,pipeline顺序对item进行处理。
一般用于:
清洗,验证,检查数据;
存储数据;
2.使用
将数据保存到json文件中示例
import json
class JsonWriterPipeline(object):
def open_spider(self, spider):
self.file = open('items.jl', 'w')
def close_spider(self, spider):
self.file.close()
def process_item(self, item, spider):
line = json.dumps(dict(item)) + "\n"
self.file.write(line)
return item
3.类及方法介绍
process_item(self, item, spider)
This method is called for every item pipeline component. process_item() must either: return a dict with data, return an Item (or any descendant class) object, return a Twisted Deferred or raise DropItem exception. Dropped items are no longer processed by further pipeline components.
Parameters:
item (Item object or a dict) – the item scraped
spider (Spider object) – the spider which scraped the item
偶尔也会使用以下方法:
open_spider(self, spider)
This method is called when the spider is opened.
Parameters: spider (Spider object) – the spider which was opened
close_spider(self, spider)
This method is called when the spider is closed.
Parameters: spider (Spider object) – the spider which was closed
from_crawler(cls, crawler)
If present, this classmethod is called to create a pipeline instance from a Crawler. It must return a new instance of the pipeline. Crawler object provides access to all Scrapy core components like settings and signals; it is a way for pipeline to access them and hook its functionality into Scrapy.
Parameters: crawler (Crawler object) – crawler that uses this pipeline
4.更多用法
激活pipeline
如果想要使用pipeline,需要在settings文件中设置如下:
ITEM_PIPELINES = {
'myproject.pipelines.PricePipeline' : 300 ,
'myproject.pipelines.JsonWriterPipeline' : 800 ,
}
数值决定运行顺序,越小越优先,设置范围为0-1000。
笔记-scrapy-pipeline的更多相关文章
- Scrapy笔记06- Item Pipeline
Scrapy笔记06- Item Pipeline 当一个item被蜘蛛爬取到之后会被发送给Item Pipeline,然后多个组件按照顺序处理这个item. 每个Item Pipeline组件其实就 ...
- 笔记-scrapy与twisted
笔记-scrapy与twisted Scrapy使用了Twisted作为框架,Twisted有些特殊的地方是它是事件驱动的,并且比较适合异步的代码. 在任何情况下,都不要写阻塞的代码.阻塞的代码包括: ...
- scrapy pipeline
pipeline的四个方法 @classmethod def from_crawler(cls, crawler): """ 初始化的时候,用以创建pipeline对象 ...
- redis学习笔记之pipeline
redis是一个cs模式的tcp server,使用和http类似的请求响应协议.一个client可以通过一个socket连接发起多个请求命令.每个请求命令发出后client通常 会阻塞并等待redi ...
- scrapy Pipeline使用twisted异步实现mysql数据插入
from twisted.enterprise import adbapi class MySQLAsyncPipeline: def open_spider(self, spider): db = ...
- scrapy Pipeline 练习
class WeatherPipeline(object): def process_item(self, item, spider): print(item) return item #插入到red ...
- Scrapy 初体验
开发笔记 Scrapy 初体验 scrapy startproject project_name 创建工程 scrapy genspider -t basic spider_name website. ...
- Python Scrapy环境配置教程+使用Scrapy爬取李毅吧内容
Python爬虫框架Scrapy Scrapy框架 1.Scrapy框架安装 直接通过这里安装scrapy会提示报错: error: Microsoft Visual C++ 14.0 is requ ...
- scrapy项目5:爬取ajax形式加载的数据,并用ImagePipeline保存图片
1.目标分析: 我们想要获取的数据为如下图: 1).每本书的名称 2).每本书的价格 3).每本书的简介 2.网页分析: 网站url:http://e.dangdang.com/list-WY1-dd ...
- Scrapy 下载文件和图片
我们学习了从网页中爬取信息的方法,这只是爬虫最典型的一种应用,除此之外,下载文件也是实际应用中很常见的一种需求,例如使用爬虫爬取网站中的图片.视频.WORD文档.PDF文件.压缩包等. 1.Files ...
随机推荐
- Smile with face. Smile with mind.
Smile with face. Smile with mind.微笑不仅是挂在脸上的,更是发自心底的.
- 关于css实现单行、多行省略标记
实现单行: overflow: hidden; text-overflow:ellipsis; white-space: nowrap; 实现多行: display: -webkit-box; -we ...
- 海量数据去重之SimHash算法简介和应用
SimHash是什么 SimHash是Google在2007年发表的论文<Detecting Near-Duplicates for Web Crawling >中提到的一种指纹生成算法或 ...
- 【Microsoft Azure学习之旅】测试消息队列(Service Bus Queue)是否会丢消息
组里最近遇到一个问题,微软的Azure Service Bus Queue是否可靠?是否会出现丢失消息的情况? 具体缘由如下, 由于开发的产品是SaaS产品,为防止消息丢失,跨Module消息传递使用 ...
- 手机APP 后端设计
原则: 命名知其意. 一看api名字就知道这个api是干啥. api返回数据禁止null 服务器动态处理原图(如 60x60 .80x80). 例如,客户端需要图片(http://w ...
- Java中的字符集
Java中的字符集 1.字符集概述 字符集是各国家文字与字符编码对照表.字符可以看成是计算机中展示的图案效果,每个字符集都对每一种图案进行编码,有着一对一的对应关系.因此进行字符输出时,都需要指定使用 ...
- robotframework实战三--自定义关键字
在rf的实战1中,我的登录获取验证码就使用了自定义关键字,具体怎么做的,如下 1.新建文件夹 新建一个文件夹,我的MyLibrary,并且存放在site-packages下 2.编写代码 在pytho ...
- ELF文件中section与segment的区别
http://blog.csdn.net/joker0910/article/details/7655606 1. ELF中的section主要提供给Linker使用, 而segment提供给Load ...
- Gym 100090D Insomnia
从 n 变到 1,有多少种方案? 打表记忆化. #include <bits/stdc++.h> using namespace std; int n; ]; int dfs(int n) ...
- Nodejs事件监听模块
nodejs里面是不存在浏览器里面都冒泡,捕获这些行为的,所以Nodejs实现了events这个模块,里面大多数的模块都集成了这个模块,所以events是node模块里面最重要都一个模块,他对外只暴露 ...