我想使用一个参数' term'在我的_init_
方法中。正如您所看到的,当_init
方法获取此参数时,我想将其用作我的数据库集合的名称并将数据插入其中。但是我不知道如何在整个班级中使用term
_init
的价值。所以有人对我有好的建议来处理这个问题吗?
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
答案 0 :(得分:4)
将其设为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)
# ...