1

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
1
  • Before writing classes, you should read and study at least the Python tutorial on that subject. Commented Apr 21, 2016 at 3:55

1 Answer 1

4

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)
    # ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.