AttributeError:' CSVPipeline'对象没有属性' spider_closed'

时间:2017-01-22 08:26:16

标签: python scrapy-pipeline

我按照http://stackoverflow.com/questions/20753358/how-can-i-use-the-fields-to-export-attribute-in-baseitemexporter-to-order-my-scr/20758558#20758558中的说明尝试导出项目'按顺序排列。但终端只是输出 crawler.signals.connect(pipeline.spider_closed, signals.spider_closed) AttributeError: 'CSVPipeline' object has no attribute 'spider_closed'

这是我的pipelines.py

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

class CSVPipeline(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('%s_items.csv' % spider.name, 'w+b')
        self.files[spider] = file
        self.exporter = CsvItemExporter(file)
        self.exporter.fields_to_export = ['c_name','c_employeeN','c_website','c_street','c_state_zip_code','c_country','p1c_name','p1c_role','p1c_phoneNumber','p1c_email','p2c_name','p2c_role','p2c_phoneNumber','p2c_email','p3c_name','p3c_role','p3c_phoneNumber','p3c_email','p1h_name','p1h_role','p1h_phoneNumber','p1h_email','p2h_name','p2h_role','p2h_phoneNumber','p2h_email','p3h_name','p3h_role','p3h_phoneNumber','p3h_email','p1f_name','p1f_role','p1f_phoneNumber','p1f_email','p2f_name','p2f_role','p2f_phoneNumber','p2f_email']
        self.exporter.start_exporting()

    def spider_cloesd(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

我在ITEM_PIPELINES = {'zoominfo_test.pipelines.CSVPipeline': 300,}

中添加了setting.py

我只是不知道发生了什么。有人可以给我一个提示,将受到高度赞赏。谢谢!

1 个答案:

答案 0 :(得分:1)

你的方法名称

显然有拼写错误
import matplotlib.pyplot as plt
import numpy as np

image = plt.imread("https://i.stack.imgur.com/9qe6z.png")

ar= np.zeros((image.shape[0],image.shape[1]) )
ar[100:300,50:150] = np.ones((200,100))
ar[:,322:] = np.zeros((image.shape[0],image.shape[1]-322) )*np.nan

fig,ax=plt.subplots()
ax.imshow(image)
ax.imshow(ar, alpha=0.5, cmap="RdBu")

plt.show()