[英]Change Scrapy JSON Output
我正在與Scrapy一起從管道中的蜘蛛中導出JSON。 我想將json包裝在產品對象中。
我正在使用JsonLinesItemExporter
目前,我的JSON如下所示:
{"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"
}
那么如何將其包裝在Product對象中?
這是我的管道代碼:
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
我可以使用以下代碼執行此操作:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.