Advertisement
Guest User

inspect_reponse

a guest
Sep 24th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from scrapy.spider import BaseSpider
  2. from scrapy.selector import HtmlXPathSelector
  3. from scrapy.http import FormRequest
  4. from scrapy.http.cookies import CookieJar
  5. from scrapy.shell import inspect_response       # Facilitates breaking out into a shell (debug)
  6. from scrapy.utils.response import open_in_browser
  7. from scrapy import log
  8.  
  9.  
  10. class LKQSweepsEntrySpider(BaseSpider):
  11.     '''
  12.    Enters the LKQ daily contest automagically
  13.    scrapy crawl --loglevel=WARNING lkqsweepsentry
  14.    '''
  15.     name = 'lkqsweepsentry'
  16.     allowed_domains = ["lkqpickyourpartsweeps.com"]
  17.     start_urls = ['http://www.lkqpickyourpartsweeps.com/lkq13c/pickyourpart/enter_picks']
  18.     usernames = ['test1@gmail.com', 'test2@gmail.com']
  19.     password = 'password'
  20.  
  21.     def parse(self, response):
  22.         for jar,username in enumerate(self.usernames):
  23.             self.log('Attempting entry with %s'%username, level=log.INFO)
  24.             yield FormRequest.from_response(response,
  25.                     formdata={'username': username, 'password': self.password},
  26.                     callback=self.after_login,
  27.                     dont_filter=True, # Disable filtering of duplicate URLs
  28.                     meta = {'username': username, 'cookiejar': jar})
  29.  
  30.     def after_login(self, response):
  31.         # verify login success and hit the submit button
  32.         username = response.meta['username']
  33.         self.log('Cookie jar for %s is #%s'%(username, response.meta['cookiejar']), level=log.INFO)
  34.         hxs = HtmlXPathSelector(response)
  35.         submitButtonXPath = '//*[@id="enter_picks-form-submit"]'
  36.  
  37.         if hxs.select(submitButtonXPath).extract():
  38.             # We got the submit button, let's click it to enter
  39.             self.log("Successfully logged in with %s"%username, level=log.INFO)
  40.             yield FormRequest.from_response(response,
  41.                     formname='enter_picks_form',
  42.                     formdata={},
  43.                     callback=self.after_submit,
  44.                     dont_filter=True, # Disable filtering of duplicate URLs
  45.                     meta={'username': username, 'cookiejar': response.meta['cookiejar']})
  46.         else:
  47.             # There was some sort of error
  48.             loginStatus = hxs.select('//*[@id="user-access-note"]/text()').extract()[1]
  49.             if "Invalid login" in loginStatus:
  50.                 self.log("Login failed for %s"%username, level=log.ERROR)
  51.             elif "already entered" in loginStatus:
  52.                 self.log("%s already entered the contest, aborting!"%username, level=log.WARNING)
  53.  
  54.     def after_submit(self, response):
  55.         # verify login success and hit the submit button
  56.         print response.meta
  57.         inspect_response(response)
  58.         username = response.meta['username']
  59.         hxs = HtmlXPathSelector(response)
  60.         submissionStatus = hxs.select('//*[@id="pe-content"]/div[1]/p[1]').extract()
  61.         if 'submission has been received' in submissionStatus:
  62.             self.log("Successful Entry for %s"%username, level=log.INFO)
  63.         else:
  64.             self.log("Unsuccessful Entry for %s"%username, level=log.ERROR)
  65.             print response.body
  66.         return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement