mardi 19 janvier 2016

Can not call build_in function

In the following program, I tried to call self.start_requests() in self.after_login(), but not succeed. I rewrite the content of function self.start_requests() instead and it works.

My question is that I don't understand why I can just directly call function self.start_requests()?

    __author__ = 'parallels'
    import scrapy
    from scrapy import Request
    from bs4 import BeautifulSoup

    def start_requests(usrname, password):
        return Request(url="http://ift.tt/1Jgm8QL",
                       cookies={'name':usrname, 'password':password},dont_filter = True)


    class heibanke2(scrapy.Spider):
        name = "herbanke2"
    #     start_urls = ["http://ift.tt/1Jgm8QL"]
        password = 4

        def start_requests(self):
            return [Request("http://ift.tt/1Jgm8QL", callback = self.post_login,dont_filter = True)]

        #FormRequeset
        def post_login(self, response):
            print 'Preparing login'
            print "current password:" , str(self.password)
            return [scrapy.FormRequest.from_response(response,
                                formdata = {
                                'username': "JoseLyn",
                                'password': str(self.password)
                                },
                                callback = self.after_login
                                )]
        def after_login(self,response):
            print "after_login"
            with open("body" + str(self.password),"wb") as f:
                f.write(response.body)
            soup = BeautifulSoup(response.body,"lxml")
            if "JoseLyn" not in soup.h3.string:
                self.password += 1
     #           self.start_requests()
                return [Request("http://ift.tt/1Jgm8QL", callback = self.post_login,dont_filter = True)]

            else:
                print "password found:", str(self.password)
                print "next mission at:", 'http://www.heibanke.com' + soup.a['href']

Thank you in advance!




Aucun commentaire:

Enregistrer un commentaire