Reputation: 1337
I want to use one parameter 'term' in my _init_
method.As you can see, when the _init
method get this parameter, I want to use it as the name of my database's collection and insert data into it. But I don't know how to use the value of term
from _init
in the whole class.So does anybody have good advice for me to deal with this?
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
Upvotes: 1
Views: 51
Reputation: 474221
Make it an instance variable:
def __init__(self, term=None, *args, **kwargs):
super(EPGDspider, self).__init__(*args, **kwargs)
self.term = term
# ...
Then, reference it using self.term
in other methods:
def parse(self, response):
print(self.term)
# ...
Upvotes: 4