笔记-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的更多相关文章

  1. Scrapy笔记06- Item Pipeline

    Scrapy笔记06- Item Pipeline 当一个item被蜘蛛爬取到之后会被发送给Item Pipeline,然后多个组件按照顺序处理这个item. 每个Item Pipeline组件其实就 ...

  2. 笔记-scrapy与twisted

    笔记-scrapy与twisted Scrapy使用了Twisted作为框架,Twisted有些特殊的地方是它是事件驱动的,并且比较适合异步的代码. 在任何情况下,都不要写阻塞的代码.阻塞的代码包括: ...

  3. scrapy pipeline

    pipeline的四个方法 @classmethod def from_crawler(cls, crawler): """ 初始化的时候,用以创建pipeline对象 ...

  4. redis学习笔记之pipeline

    redis是一个cs模式的tcp server,使用和http类似的请求响应协议.一个client可以通过一个socket连接发起多个请求命令.每个请求命令发出后client通常 会阻塞并等待redi ...

  5. scrapy Pipeline使用twisted异步实现mysql数据插入

    from twisted.enterprise import adbapi class MySQLAsyncPipeline: def open_spider(self, spider): db = ...

  6. scrapy Pipeline 练习

    class WeatherPipeline(object): def process_item(self, item, spider): print(item) return item #插入到red ...

  7. Scrapy 初体验

    开发笔记 Scrapy 初体验 scrapy startproject project_name 创建工程 scrapy genspider -t basic spider_name website. ...

  8. Python Scrapy环境配置教程+使用Scrapy爬取李毅吧内容

    Python爬虫框架Scrapy Scrapy框架 1.Scrapy框架安装 直接通过这里安装scrapy会提示报错: error: Microsoft Visual C++ 14.0 is requ ...

  9. scrapy项目5:爬取ajax形式加载的数据,并用ImagePipeline保存图片

    1.目标分析: 我们想要获取的数据为如下图: 1).每本书的名称 2).每本书的价格 3).每本书的简介 2.网页分析: 网站url:http://e.dangdang.com/list-WY1-dd ...

  10. Scrapy 下载文件和图片

    我们学习了从网页中爬取信息的方法,这只是爬虫最典型的一种应用,除此之外,下载文件也是实际应用中很常见的一种需求,例如使用爬虫爬取网站中的图片.视频.WORD文档.PDF文件.压缩包等. 1.Files ...

随机推荐

  1. Gradle 教程:第二部分,JAVA PROJECTS【翻译】

    原文地址:http://rominirani.com/2014/07/28/gradle-tutorial-part-2-java-projects/ 在这部分的教学中,我们会学习如何使用Gradle ...

  2. Miner3D Basic基础版

    ——强大的数据可视化软件 数据分析并不很复杂,Miner3D Basic基础版首先使用简单的方法,创造了强劲的图形驱动的数据处理模型,然后通过一个完整的视图为基本的交互环境,对基本数据进行分析,并通过 ...

  3. Android设置TextView的行间距,EditText下划线

    textView用于显示文本,大量文字显示在一起显得过于紧凑.可通过在布局中更改TextView属性设置行间距. 1.android:lineSpacingMultiplier="1.5&q ...

  4. 爱上python(几个小例子)

    1.任务:简单测试局域网中的网络是否连接,ip范围:192.168.2.101到192.168.2.200. python 实现代码: import subprocess cmd="cmd. ...

  5. JDK、JRE、javac和JVM的关系

      .java为Java的源文件后缀,编写的代码需要写在.java文件中.     Javac编译器,用于读取Java源代码,并将其编译成字节代码.经过javac编译后形成.class,是字节码文件. ...

  6. ES6相关特性(解构赋值)

    解构赋值:本质上是一种匹配模式,等号两边的模式相同,则左边的变量可以被赋予对应的值. 注意:null & undefined 不能解构赋值!!! 数组的解构赋值: let [a,[[b],c] ...

  7. 分享个谷歌浏览器下的一款插件PostMan

    用作POST GET调试非常好用 先下载谷歌浏览器 然后在应用里搜索安装即可

  8. pt-table-sync

    高效的同步MySQL表之间的数据,他可以做单向和双向同步的表数据.他可以同步单个表,也可以同步整个库.它不同步表结构.索引.或任何其他模式对象.所以在修复一致性之前需要保证他们表存在.   使用范例: ...

  9. 前端高质量知识(二)-JS执行上下文(执行环境)详细图解Script

    先随便放张图 我们在JS学习初期或者面试的时候常常会遇到考核变量提升的思考题.比如先来一个简单一点的. console.log(a); // 这里会打印出什么? var a = 20; PS: 变量提 ...

  10. F​l​a​s​h​ ​M​e​d​i​a​ ​L​i​v​e​ ​E​n​c​o​d​e​r​参​数​表

    Flash Media Live Encoder命令行推流Flash Media Live Encoder NotesFlash Media Live Encoder 除了直接以 GUI 方式操作之外 ...