【问题标题】:Scrapy: How to output items in a specific json formatScrapy:如何以特定的 json 格式输出项目
【发布时间】:2017-08-18 19:40:42
【问题描述】:

我以 json 格式输出抓取的数据。 默认的 scrapy 导出器以 json 格式输出 dict 列表。 项目类型如下:

[{"Product Name":"Product1", "Categories":["Clothing","Top"], "Price":"20.5", "Currency":"USD"},
{"Product Name":"Product2", "Categories":["Clothing","Top"], "Price":"21.5", "Currency":"USD"},
{"Product Name":"Product3", "Categories":["Clothing","Top"], "Price":"22.5", "Currency":"USD"},
{"Product Name":"Product4", "Categories":["Clothing","Top"], "Price":"23.5", "Currency":"USD"}, ...]

但我想以这样的特定格式导出数据:

{
"Shop Name":"Shop 1",
"Location":"XXXXXXXXX",
"Contact":"XXXX-XXXXX",
"Products":
[{"Product Name":"Product1", "Categories":["Clothing","Top"], "Price":"20.5", "Currency":"USD"},
{"Product Name":"Product2", "Categories":["Clothing","Top"], "Price":"21.5", "Currency":"USD"},
{"Product Name":"Product3", "Categories":["Clothing","Top"], "Price":"22.5", "Currency":"USD"},
{"Product Name":"Product4", "Categories":["Clothing","Top"], "Price":"23.5", "Currency":"USD"}, ...]
}

请告诉我任何解决方案。 谢谢。

【问题讨论】:

  • 您希望从哪里获得Shop NameLocation...?你每次都要手动定义吗?
  • 不,我在蜘蛛开始运行时收到它作为参数。
  • 那你能告诉我们更多关于如何接收和定义参数的信息吗?你的一些代码会有所帮助。
  • 我还没有实现它。我要读取 csv 文件存储参数列表。
  • 所以{"Product Name":"Product1", "Categories":["Clothing","Top"], "Price":"20.5", "Currency":"USD"} 会是一个scrapy 项目,对吧?并且您想在最终文件中包含Shop Name, Location, etc.

标签: python json scrapy


【解决方案1】:

这在scrapy网页here有很好的记录。

from scrapy.exporters import JsonItemExporter


class ItemPipeline(object):

    file = None

    def open_spider(self, spider):
        self.file = open('item.json', 'w')
        self.exporter = JsonItemExporter(self.file)
        self.exporter.start_exporting()

    def close_spider(self, spider):
        self.exporter.finish_exporting()
        self.file.close()

    def process_item(self, item, spider):
        self.exporter.export_item(item)
        return item

这将创建一个包含您的项目的 json 文件。

【讨论】:

  • 我要做的就是美化从 python 脚本中吐出的json 文件。这行得通吗?!
  • @Anthony 正如您在JsonItemExporter docs 中看到的那样,该类接受kwargs 参数。我在github上看了一会儿源码,发现这个类中有一个self.indent属性。所以你需要 JsonItemExporter(self.file, indent=2) 来获得一个漂亮的 Json 格式(据我所知,这没有经过测试)。请让我知道这是否有效。
  • 经过一些调整后,这对我有用 (1) 激活生成的设置 ​​ITEM_PIPELINES (2) 以二进制形式打开文件,以避免builtins.TypeError: write() argument must be str, not bytes (3-可选) 以蜘蛛命名输出文件:self.file = open(spider.name + '.json', 'wb')
  • @Gocht JsonItemExporter(self.file, indent=2) 然后效果很好!
【解决方案2】:

我试图导出漂亮的打印 JSON,这对我有用。

我创建了一个如下所示的管道:

class JsonPipeline(object):

    def open_spider(self, spider):
        self.file = open('your_file_name.json', 'wb')
        self.file.write("[")

    def close_spider(self, spider):
        self.file.write("]")
        self.file.close()

    def process_item(self, item, spider):
        line = json.dumps(
            dict(item),
            sort_keys=True,
            indent=4,
            separators=(',', ': ')
        ) + ",\n"

        self.file.write(line)
        return item

它类似于 scrapy 文档 https://doc.scrapy.org/en/latest/topics/item-pipeline.html 中的示例,不同之处在于它打印每个缩进的 JSON 属性并在新行上。

在这里查看关于漂亮打印的部分https://docs.python.org/2/library/json.html

【讨论】:

  • 什么是your_file_name.json
  • @Max 在文件末尾添加/打印一个额外的,。它不是一个有效的 json 文件。
  • 为什么是wb 模式?这不是说writebinary吗??
猜你喜欢
  • 1970-01-01
  • 2016-07-25
  • 2022-11-15
  • 2016-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-29
  • 2019-02-21
相关资源
最近更新 更多