Scrapy export JSON 中文 unicode 問題

You can change the setting.py

FEED_EXPORT_ENCODING='utf-8' , 可按下去看doc


綱上好多人都教自己寫openfile,  但我覺得如果framework 有setting 改,不是用framework setting 簡單多嗎,
以下code 是綱上好多教法 https://stackoverflow.com/questions/9181214/scrapy-text-encoding


class 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()

留言