Change Scrapy JSON Output

2019-09-02 10:45发布

I am working with Scrapy to export JSON from my spider in the pipeline. I want to wrap the json in a product object.

I am using the JsonLinesItemExporter

Currently, my JSON looks like this:

{"name": "Protective iPhone Stand Case",
    "link": "https://things.com/899029978367138670/Strap-On-SoftRack-Roof-Rack-by-Otium",
    "category_old": "Sports & Outdoors",
    "image_url": "https://thingd-media-ec1.com/default/899029978367138670_42120cf10765.jpg",
    "price": "160",
    "interest": "13",
    "company": "ACME",
    "country": "USA"}

"product": {
    "name": "Protective iPhone Stand Case",
    "link": "https://things.com/899029978367138670/Strap-On-SoftRack-Roof-Rack-by-Otium",
    "category_old": "Sports & Outdoors",
    "image_url": "https://thingd-media-ec1.com/default/899029978367138670_42120cf10765.jpg",
    "price": "160",
    "interest": "13",
    "company": "ACME",
    "country": "USA"
}

So how do I wrap it in the Product object?

Here is my pipelines code:

import requests
import time
from scrapy.utils.project import get_project_settings
import sys
import json
from scrapy import signals
from scrapy.exporters import JsonLinesItemExporter

SETTINGS = get_project_settings()

class FancyPipeline(object):

  def __init__(self):
        #Instantiate API Connection
        self.files = {}
        url = 'http://unshakable-missile-106309.nitrousapp.com:3000/api/v1/imports'

  @classmethod
  def from_crawler(cls, crawler):
        pipeline = cls()
        crawler.signals.connect(pipeline.spider_opened, signals.spider_opened)
        crawler.signals.connect(pipeline.spider_closed, signals.spider_closed)
        return pipeline

  def spider_opened(self, spider):
        #open a static/dynamic file to read and write to
        file = open('%s_items.json' % spider.name, 'w+b')
        self.files[spider] = file
        self.exporter = JsonLinesItemExporter(file)
        self.exporter.start_exporting()

  def spider_closed(self, spider):
        self.exporter.finish_exporting()
        file = self.files.pop(spider)
        file.close()

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

1条回答
Summer. ? 凉城
2楼-- · 2019-09-02 11:05

I was able to do this with the following code:

def spider_opened(self, spider):
        #open a static/dynamic file to read and write to
        file = open('%s_items.json' % spider.name, 'w+b')
        self.files[spider] = file
        file.write('''{
    "product": [''')
        self.exporter = JsonLinesItemExporter(file)
        self.exporter.start_exporting()

  def spider_closed(self, spider):
        self.exporter.finish_exporting()
        file = self.files.pop(spider)
        file.write("]}")
        file.close()
查看更多
登录 后发表回答