小编Jan*_*lmi的帖子

Scrapy 中的 .json 导出格式

只是一个关于 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)

python json export scrapy scrapy-pipeline

2
推荐指数
1
解决办法
1525
查看次数

标签 统计

export ×1

json ×1

python ×1

scrapy ×1

scrapy-pipeline ×1