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