笔记-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. Android 5.0 以上监听网络变化

    大家好,大概有一个多月没有更新博客了,我是干什么去了呢?很明显,程序员当然要加班……这一次跟大家分享一下新项目的一些心得. 监听网络变化在开发中是经常用到的,例如我们断网有一些友好的提示,或者根据不同 ...

  2. 《ArcGIS Runtime SDK for Android开发笔记》——(7)、示例代码arcgis-runtime-samples-android的使用

    1.前言 学习ArcGIS Runtime SDK开发,其实最推荐的学习方式是直接看官方的教程.示例代码和帮助文档,因为官方的示例一般来说都是目前技术最新,也是最详尽的.对于ArcGIS Runtim ...

  3. open ssh 常用的东西

    清除已经存在的但是不同设备的连接信息 ssh-keygen -f "/users/he/.ssh/known_hosts" -R 192.168.1.118 无密码登录openss ...

  4. mongoose添加属性问题

    在项目中遇到这样一个问题. 项目地址: https://github.com/ccyinghua/vue-node-mongodb-project/blob/master/07-shoppingCar ...

  5. 微信小程序实现获得用户手机号

    具体操作方法如下: 使用方法 需要将 <button> 组件 open-type 的值设置为 getPhoneNumber,当用户点击并同意之后,可以通过 bindgetphonenumb ...

  6. linux挂载和卸载NAS操作

    1.建立准备挂载NFS的目录,例如:cd /home/test,mkdir my_NFS_Catalog 2.挂接NFS至 /home/test/my_NFS_Catalog目录下(nas有两种格式: ...

  7. 在IE中解决当前安全设置不允许下载该文件的方案

    解决方案一: 1.0打开IE后,单击菜单栏中的“工具”菜单,在弹出的菜单中选择“Internet选项”命令: 2.0在弹出“Internet选项”的对话框中,打开“Internet选项”对话框: 3. ...

  8. 【2017-07-01】Linux应用开发工程师面试问题记录之二:关于结构体的大小及内存对齐问题

    Tencent后台服务器开发有一道题是计算一个结构体的sizeof的大小: struct strData { int m_Int; char m_Char; short m_Short; char m ...

  9. 正则表达式转换python2的print为python3风格

    直接查找 print ([^\n\(]*)替换为 print($1)

  10. iOS开发:小技巧积累

    1.获取全局的Delegate对象,这样我们可以调用这个对象里的方法和变量: [(MyAppDelegate*)[[UIApplication sharedApplication] delegate] ...