scrapy学习: 将 Pipelines 放在目录里,分成不同的文件来执行

Scrapy Pipelines to Seperate Folder/Files - Abstraction
更新于: 2022-01-11 07:19:09

先看正常的  scrapy 项目结构

$ scrapy startproject multipipeline
$ tree
.
├── multipipeline
│   ├── __init__.py
│   ├── items.py
│   ├── middlewares.py
│   ├── pipelines.py
│   ├── settings.py
│   └── spiders
│       ├── example.py
│       └── __init__.py
└── scrapy.cfg

这个时候你的  pipelines.py 如下

$ cat multipipeline/pipelines.py 
# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html


class MultipipelinePipeline(object):
    def process_item(self, item, spider):
        return item

要想将 piplines 变成一个目录,就可以这样

$ tree
.
├── multipipeline
│   ├── __init__.py
│   ├── items.py
│   ├── middlewares.py
│   ├── pipelines
│   │   ├── __init__.py
│   │   ├── one.py
│   │   ├── three.py
│   │   └── two.py
│   ├── settings.py
│   └── spiders
│       ├── example.py
│       └── __init__.py
└── scrapy.cfg

settings.py 修改如下

ITEM_PIPELINES = {
    'multipipeline.pipelines.one.MyPipelineOne': 100,
    'multipipeline.pipelines.two.MyPipelineTwo': 200,
    'multipipeline.pipelines.three.MyPipelineThree': 300,
}

参考

https://stackoverflow.com/questions/44302127/scrapy-pipelines-to-seperate-folder-files-abstraction