我有一个带有多个蜘蛛的Scrapy项目以及多个管道.有没有办法告诉蜘蛛A使用管道A等?
我的pipelines.py有多个管道类,每个管道类都做了不同的事情,我希望能够告诉蜘蛛使用特定的管道.
我没有看到任何明显的方法来查看可用的scrapy命令来执行此操作...
小智 8
可以在spider类的custom_settings属性中指定要使用的管道:
class BookSpider(BaseSpider):
name = "book_spider"
custom_settings = {
'ITEM_PIPELINES': {
'my_app.pipelines.BookPipeline': 300,
}
}
def parse(self, response):
return
Run Code Online (Sandbox Code Playgroud)
ITEM_PIPELINES在引擎启动期间,为项目中的所有蜘蛛全局定义设置.它不能在飞行中每个蜘蛛改变.
这是你可以做的.通过管道本身的管道定义应该处理哪些蜘蛛.跳过/继续处理由管道的process_item方法中的蜘蛛返回的项目,例如:
def process_item(self, item, spider):
if spider.name not in ['spider1', 'spider2']:
return item
# process item
Run Code Online (Sandbox Code Playgroud)
另见:
希望有所帮助.