将scrapy实例(非类)属性传递给管道

时间:2017-12-24 19:18:32

标签: python scrapy

我正在使用scrapy。我的pipieline开头是:

class DynamicSQLlitePipeline(object):

    @classmethod
    def from_crawler(cls, crawler):
        # Here, you get whatever value was passed through the "table" parameter
        table = getattr(crawler.spider, "table")
        return cls(table)

    def __init__(self,table):
        try:
            db_path = "sqlite:///"+settings.SETTINGS_PATH+"\\data.db"
            db = dataset.connect(db_path)
            table_name = table[0:3]  # FIRST 3 LETTERS
            self.my_table = db[table_name]

我的蜘蛛开始于:

class For_Spider(Spider):

    name = "for"
    table = 'hello' # creating dummy attribute. will be overwritten

    def start_requests(self):

        self.table = self.dc # dc is passed in

当我用:

启动蜘蛛
scrapy crawl for -a dc=mystring -a records=1

dc作为实例属性(不是类属性)传入

如何将此实例属性传递给我的管道,以便我可以在管道中动态设置表名?我现在拥有的类属性。

0 个答案:

没有答案