只是一个关于 Scrapy 中 json 导出格式的快速问题。我导出的文件看起来像这样。
{"pages": {"title": "x", "text": "x", "tags": "x", "url": "x"}}
{"pages": {"title": "x", "text": "x", "tags": "x", "url": "x"}}
{"pages": {"title": "x", "text": "x", "tags": "x", "url": "x"}}
Run Code Online (Sandbox Code Playgroud)
但我希望它采用这种确切的格式。不知何故,我需要在“页面”下获取所有其他信息。
{"pages": [
{"title": "x", "text": "x", "tags": "x", "url": "x"},
{"title": "x", "text": "x", "tags": "x", "url": "x"},
{"title": "x", "text": "x", "tags": "x", "url": "x"}
]}
Run Code Online (Sandbox Code Playgroud)
我在scrapy或python方面不是很有经验,但除了导出格式之外,我已经在我的蜘蛛中完成了所有其他工作。这是我的pipelines.py,我刚开始工作。
from scrapy.exporters import JsonItemExporter
import json
class RautahakuPipeline(object):
def open_spider(self, spider):
self.file = open('items.json', 'w')
def close_spider(self, spider):
self.file.close() …Run Code Online (Sandbox Code Playgroud)