自定义pipeline将item导出为json
import codecs
import json
class JsonWithEncodingPipeline(object):
"""docstring for JsonWithEncodingPipeline"""
#自定义json文件的导出
def __init__(self):
#w 以写的方式打开,会先把文件清空,再写入; a append,以追加的方式写入
self.file = codecs.open("data.json","w",encoding="utf-8")
#scrapy有很多固定好的方法签名,我们不要随便去改动
def process_item(self,item,spider):
lines = json.dumps(dict(item),ensure_ascii=False)+"\n"
self.file.write(lines)
pass
return item
def spider_closed(self,spider):
self.file.close()
接着把这个pipeline配置到setting中