如何在其他方法中使用“_init_”的参数?

How to use the arguments of "_init_ " in other methods?

我想在我的_init_中使用一个参数'term' method.As你可以看到,当_init方法得到这个参数时,我想把它用作我的数据库集合的名称并将数据插入其中。但是我不知道如何在整个过程中使用 _init 中的 term 的值 class.So 有人对我有什么好的建议来处理这个问题吗?

class EPGDspider(scrapy.Spider):
        name = "EPGD"
        allowed_domains = ["epgd.biosino.org"]

    db = DB_Con()
    collection = db.getcollection(term)

    def __init__(self, term=None, *args, **kwargs):
        super(EPGDspider, self).__init__(*args, **kwargs)
        self.start_urls = ['http://epgd.biosino.org/EPGD/search/textsearch.jsp?textquery=%s&submit=Feeling+Lucky' % term]

    def parse(self, response):
        sel = Selector(response)
        sites = sel.xpath('//tr[@class="odd"]|//tr[@class="even"]')
        url_list = []
        base_url = "http://epgd.biosino.org/EPGD"

        for site in sites:
            item = EPGD()
            item['description'] = map(unicode.strip, site.xpath('td[6]/text()').extract())
            self.collection.update({"genID": item['genID']}, dict(item), upsert=True)
            yield item

设为 instance variable:

def __init__(self, term=None, *args, **kwargs):
    super(EPGDspider, self).__init__(*args, **kwargs)

    self.term = term
    # ...

然后,在其他方法中使用self.term引用它:

def parse(self, response):
    print(self.term)
    # ...