Scrapy custom exporter

后端 未结 3 829
野趣味
野趣味 2021-02-06 09:08

I am defining an item exporter that pushes items to a message queue. Below is the code.

from scrapy.contrib.exporter import JsonLinesItemExporter
from scrapy.uti         


        
3条回答
  •  余生分开走
    2021-02-06 09:46

    "Feed Exporters" are quick (and somehow dirty) shortcuts to call some "standard" item exporters. Instead of setting up a feed exporter from settings, hard wire your custom item exporter to your custom pipeline, as explained here http://doc.scrapy.org/en/0.14/topics/exporters.html#using-item-exporters :

    from scrapy.xlib.pydispatch import dispatcher
    from scrapy import signals
    from scrapy.contrib.exporter import XmlItemExporter
    
    class MyPipeline(object):
    
        def __init__(self):
            ...
            dispatcher.connect(self.spider_opened, signals.spider_opened)
            dispatcher.connect(self.spider_closed, signals.spider_closed)
            ...
    
        def spider_opened(self, spider):
            self.exporter = QueueItemExporter()
            self.exporter.start_exporting()
    
        def spider_closed(self, spider):
            self.exporter.finish_exporting()
    
        def process_item(self, item, spider):
            # YOUR STUFF HERE
            ...
            self.exporter.export_item(item)
            return item
    

提交回复
热议问题