教程 > scrapy 教程 > 阅读:33

scrapy item pipeline(管道)——迹忆客-ag捕鱼王app官网

item pipeline(管道)是一种处理报废项目的方法。 当一个项目被发送到 item pipeline 时,它会被蜘蛛抓取并使用多个组件进行处理,这些组件按顺序执行。

每当收到一个 item 时,它都会决定以下任一操作 -

  • 继续处理该项目。
  • 从管道中删除它。
  • 停止处理该项目。

项目管道通常用于以下目的

  • 将抓取的项目存储在数据库中。
  • 如果收到的物品是重复的,那么它将丢弃重复的物品。
  • 它将检查该项目是否具有目标字段。
  • 清除 html 数据。

语法

我们可以使用以下方法编写 item pipeline

process_item(self, item, spider) 

上述方法包含以下参数 -

  • item (item object or dictionary) - 它指定被抓取的项目。
  • spider (spider object) - 抓取物品的蜘蛛。

我们可以使用下表中给出的其他方法

序号 方法 描述 参数
1 open_spider(self, spider) 打开蜘蛛时执行它。 spider (spider object) − 它指的是打开的蜘蛛。
2 close_spider(self, spider) 它在蜘蛛关闭时被执行。 spider (spider object) − 它指的是关闭的蜘蛛。
3 from_crawler(cls, crawler) 借助爬虫,管道可以访问到scrapy的信号、设置等核心组件。 crawler (crawler object) − 它指的是使用这个管道的爬虫。

示例

以下是在不同概念中使用的 item 管道示例。

在以下代码中,管道会平衡那些不包含 vat (excludes_vat 属性)的项目的 (price) 属性,并忽略那些没有价格标签的项目

from scrapy.exceptions import dropitem  
class pricepipeline(object): 
   vat = 2.25 
   def process_item(self, item, spider): 
      if item['price']: 
         if item['excludes_vat']: 
            item['price'] = item['price'] * self.vat 
            return item 
         else: 
            raise dropitem("missing price in %s" % item) 

将 item 写入 json 文件

以下代码将从所有蜘蛛中抓取的所有项目存储到一个 items.jl 文件中,该文件以 json 格式的序列化形式每行包含一个项目。 代码中使用了 jsonwriterpipeline 类来展示如何编写item pipeline

import json  
class jsonwriterpipeline(object): 
   def __init__(self): 
      self.file = open('items.jl', 'wb') 
   def process_item(self, item, spider): 
      line = json.dumps(dict(item))   "\n" 
      self.file.write(line) 
      return item

将 item 写入 mongodb

你可以在scrapy设置中指定mongodb地址和数据库名称,mongodb集合可以以项目类命名。 以下代码描述了如何使用 from_crawler() 方法正确收集资源

import pymongo  
class mongopipeline(object):  
   collection_name = 'scrapy_list' 
   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_db', 'lists') 
      ) 
  
   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(dict(item)) 
      return item

复制过滤器

过滤器将检查重复的项目,并删除已经处理过的项目。 在下面的代码中,我们为我们的项目使用了一个唯一的 id,但是 spider 返回了许多具有相同 id 的项目

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("repeated items found: %s" % item) 
      else: 
         self.ids_seen.add(item['id']) 
         return item

激活item管道

我们可以通过将其类添加到 item_pipelines 设置来激活 item 管道组件,如以下代码所示。 我们可以按照类的运行顺序为类分配整数值(顺序可以是值较低的类到值较高的类),值将在 0-1000 范围内。

item_pipelines = {
   'myproject.pipelines.pricepipeline': 100,
   'myproject.pipelines.jsonwriterpipeline': 600,
}

查看笔记

扫码一下
查看教程更方便
网站地图