Scrapy框架: pipelines.py设置

保存数据到json文件

# -*- coding: utf-8 -*-

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

from scrapy.exporters import JsonItemExporter

class ErshouPipeline(object):
    def open_spider(self, spider):
        self.file=open('01ershou.json','wb')
        self.exporter=JsonItemExporter(self.file)
        self.exporter.start_exporting()

    def process_item(self, item, spider):
        self.exporter.export_item(item)
        return item

    def close_spider(self, spider):
        self.exporter.finish_exporting()
        self.file.close()
原文地址:https://www.cnblogs.com/hankleo/p/11829736.html