2015-10-22 4 views
0

Я работаю с Scrapy, чтобы экспортировать JSON из моего паука в конвейер. Я хочу обернуть json в объект продукта.Изменить Scrapy Выход 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" 
} 

Так как же я обернуть его в объекте продукта?

Вот мой трубопроводы код:

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

Я был в состоянии сделать это с помощью следующего кода:

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() 
Смежные вопросы