2

我正在使用以下管道将我的项目导出到 CSV(使用 Scrapy 0.17):

from scrapy import signals
from scrapy.contrib.exporter import CsvItemExporter

class CsvExportPipeline(object):

    def __init__(self):
        self.files = {}

    @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):
        file = open('/home/ubuntu/SCRAPING/%s_%s.csv' % (self.name, datetime.datetime.now()), 'w+b')
        self.files[spider] = file
        self.exporter = CsvItemExporter(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

但是,当我使用此管道时,出现以下错误: exceptions.AttributeError: 'CsvExportPipeline' object has no attribute 'exporter'

请建议我缺少什么?

4

1 回答 1

1

Got the solution.Problem was with using self.name when I was naming the file.It should be spider.name Huh the error message was simply misleading and took away couple of hours

于 2013-08-22T18:34:48.460 回答