Scrapy教程

Scrapy 管道

说明

Item Pipeline 是一种处理报废项目的方法。当一个项目被发送到项目管道时,它会被爬虫抓取并使用多个组件进行处理,这些组件是顺序执行的。
每当收到字段时,它会决定以下任一操作-
继续处理该项目。 将其从管道中删除。 停止处理该项目。
项目管道通常用于以下目的-
将抓取的项目存储在数据库中。 如果收到的项目是重复的,那么它会丢弃重复的项目。 它会检查项目是否有针对性字段。 清除 HTML 数据。

语法

您可以使用以下方法编写项目管道-
process_item(self, item, spider) 
上述方法包含以下参数-
Item (item object or dictionary)-它指定被抓取的项目。 spider (spider object)-抓取字段的爬虫。
您可以使用下表中给出的其他方法-
方法和说明 参数
open_spider(self, spider)
打开爬虫时选中。
spider (spider object)-它指的是被打开的爬虫。
close_spider(self, spider)
爬虫关闭时被选中。
spider (spider object)-它指的是关闭的爬虫。
from_crawler(cls, crawler)
在爬虫的帮助下,管道可以访问信号和Scrapy 的设置。
crawler (Crawler object)-它指的是使用这个管道的爬虫。

示例

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

丢弃没有标签的项目

在以下代码中,管道平衡那些不包含增值税的商品的 (price) 属性 (excludes_vat 属性) 并忽略那些不包含增值税的商品价格标签-
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) 

将项目写入 JSON 文件

以下代码将从所有爬虫中抓取的所有项目存储到一个 items.jl 文件中,该文件以 JSON 格式的序列化形式每行包含一个项目。 JsonWriterPipeline 类在代码中用于展示如何编写项目管道-
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

将项目写入 MongoDB

可以在Scrapy设置中指定MongoDB地址和数据库名称,MongoDB集合可以以item类命名。以下代码描述了如何使用 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,但爬虫返回了许多具有相同 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_PIPELINES 设置来激活 Item Pipeline 组件,如下面的代码所示。您可以按照类运行的顺序为这些类分配整数值(对于值较高的类,顺序可以较低),并且值将在 0-1000 范围内。
ITEM_PIPELINES = {
   'myproject.pipelines.PricePipeline': 100,
   'myproject.pipelines.JsonWriterPipeline': 600,
}
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4