使用json模块处理JSON数据
class JsonwithEncodingPipeline(object):

    def __init__(self):
        self.file = codecs.open('article.json', 'w', encoding='utf-8')

    def process_item(self, item, spider):
        lines = json.dumps(dict(item), ensure_ascii=False) + '\n'
        self.file.write(lines)
        return item

    def spider_closed(self, spider):
        self.file.close()

 

内置JSON处理对象JsonItemExporter的使用
class JsonExporterPipeline(object):

    def __init__(self):
        self.file = open('articleexport.json', 'wb')
        self.exporter = JsonItemExporter(self.file, encoding='utf-8', ensure_ascii=False)
        self.exporter.start_exporting()

    def spider_closed(self, spider):
        self.exporter.finish_exporting()
        self.file.close()

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

 

相关文章:

  • 2021-08-05
  • 2021-08-13
  • 2021-07-11
  • 2021-12-12
  • 2021-11-14
  • 2021-12-16
  • 2022-02-28
猜你喜欢
  • 2022-12-23
  • 2022-01-08
  • 2022-01-07
  • 2021-08-08
  • 2021-06-29
  • 2022-12-23
  • 2021-11-30
相关资源
相似解决方案