scrapy在保存json文件时容易乱码
settings.py
文件改动:
ITEM_PIPELINES = {
'tutorial.pipelines.TutorialPipeline': 300,
}
pipeline.py
文件改动:
import json
import codecs
class TutorialPipeline(object):
def __init__(self, spider):
self.file = codecs.open('data_cn.json', 'w', encoding='utf-8')
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.closOline()