解决Scrapy抓取中文结果保存为文件时的编码问题

 import json
import codecs # Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html class PandaPipeline(object):
def __init__(self):
self.file = codecs.open('pandaow.json', 'w', encoding='utf-8') def process_item(self, item, spider):
line = json.dumps(dict(item),ensure_ascii=False) + '\n'
# print line
# self.file.write(line.decode("unicode_escape"))
self.file.write(line)
return item def spider_closed(self, spider):
self.file.close()

将以上内容插入pipelines.py,同时在settings.py中加入

ITEM_PIPELINES = {
'panda.pipelines.PandaPipeline': 300
}

以调用pipelines文件

上一篇:快来领取一场专门讲解UTF-8与UTF-16编码算法的GitChat活动的免费名额


下一篇:# 20145103《Java程序设计》第6周学习总结