【发布时间】:2014-09-18 08:05:09
【问题描述】:
这是一个自定义管道
并且我想用蜘蛛名称保存文件
这是我的代码。它将创建一个 json 文件但只保存一个数据
请教我如何编辑代码。里面应该有10个数据。
class JsonWithEncodingPipeline(object):
# def __init__(self):
#save file with fixed name
# self.file = codecs.open('outputbytest.json ', 'w', encoding='utf-8')
def process_item(self, item, spider):
#How to save file with dynamic name??
self.file = codecs.open('%s_outputchiness.json' % spider.name, 'w', encoding='utf-8')
line = json.dumps(dict(item)) + "\n"
self.file.write(line.encode('utf-8').decode("unicode_escape"))
return item
def spider_closed(self, spider):
self.file.close()
【问题讨论】:
标签: python-2.7 scrapy json