笨鸟编程-零基础入门Pyhton教程

 找回密码
 立即注册

项目管道

发布者: 笨鸟自学网



重复筛选器

查找重复项并删除已处理的项的筛选器。假设我们的项目有一个唯一的ID,但是我们的spider返回具有相同ID的多个项目:

from itemadapter import ItemAdapter
from scrapy.exceptions import DropItem

class DuplicatesPipeline:

    def __init__(self):
        self.ids_seen = set()

    def process_item(self, item, spider):
        adapter = ItemAdapter(item)
        if adapter['id'] in self.ids_seen:
            raise DropItem(f"Duplicate item found: {item!r}")
        else:
            self.ids_seen.add(adapter['id'])
            return item

激活项目管道组件

若要激活项管道组件,必须将其类添加到 ITEM_PIPELINES 设置,如以下示例中所示:

ITEM_PIPELINES = {
    'myproject.pipelines.PricePipeline': 300,
    'myproject.pipelines.JsonWriterPipeline': 800,
}

在此设置中分配给类的整数值决定了它们的运行顺序:项从低值类传递到高值类。习惯上把这些数字定义在0-1000范围内。

1234
上一篇:Scrapy shell下一篇:Feed 导出

Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )

GMT+8, 2024-9-8 11:10 , Processed in 0.018973 second(s), 17 queries .

© 2001-2020

返回顶部