scrapy生成json中文乱码解决

来源:互联网 发布:js触发resize事件 编辑:程序博客网 时间:2024/06/05 14:47

ITEM_PIPELINES = [‘xxx.pipelines.JsonWithEncodingPipeline’]

# -*- coding: utf-8 -*-# Define your item pipelines here## Don't forget to add your pipeline to the ITEM_PIPELINES settingimport jsonimport codecsimport osclass JsonWithEncodingPipeline(object):    def __init__(self):        self.file = codecs.open('scraped_data_utf8.json', 'w', encoding='utf-8')        self.file.write('[')    def process_item(self, item, spider):        line = json.dumps(dict(item), ensure_ascii=False) + "\n"        self.file.write(line+',')        return item    def close_spider(self, spider):        self.file.seek(-1, os.SEEK_END)        self.file.truncate();        self.file.write(']')        self.file.close()
0 0
原创粉丝点击