Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

* add formrequest version of spider and checker #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions dynamic_scraper/spiders/django_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
settings = get_project_settings()
from scrapy.exceptions import CloseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request, FormRequest
from scrapy.xlib.pydispatch import dispatcher

from dynamic_scraper.spiders.django_base_spider import DjangoBaseSpider
Expand Down Expand Up @@ -99,3 +100,25 @@ def parse(self, response):
else:
self.log("XPath result string not found. Item kept.", log.INFO)
return

class FormRequestDjangoChecker(DjangoChecker):

def __init__(self, *args, **kwargs):
super(FormRequestDjangoChecker, self).__init__(self, *args, **kwargs)

assert 'username' in kwargs
assert 'password' in kwargs
assert 'username_form' in kwargs
assert 'password_form' in kwargs

self.username, self.password, self.username_form, self.password_form \
= kwargs['username'], kwargs['password'], \
kwargs['username_form'], kwargs['password_form']

def parse(self, response):
return FormRequest.from_response(
response,
formdata={self.username_form: self.username,
self.password_form: self.password},
callback=super(FormRequestDjangoChecker, self).parse
)
23 changes: 22 additions & 1 deletion dynamic_scraper/spiders/django_spider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from scrapy import log
from scrapy.selector import HtmlXPathSelector, XmlXPathSelector
from scrapy.http import Request
from scrapy.http import Request, FormRequest
from scrapy.contrib.loader import XPathItemLoader
from scrapy.contrib.loader.processor import TakeFirst
from scrapy.exceptions import CloseSpider
Expand Down Expand Up @@ -239,3 +239,24 @@ def parse(self, response):
else:
self.log("Detail page url elem could not be read!", log.ERROR)

class FormRequestDjangoSpider(DjangoSpider):

def __init__(self, *args, **kwargs):
super(FormRequestDjangoSpider, self).__init__(self, *args, **kwargs)

assert 'username' in kwargs
assert 'password' in kwargs
assert 'username_form' in kwargs
assert 'password_form' in kwargs

self.username, self.password, self.username_form, self.password_form \
= kwargs['username'], kwargs['password'], \
kwargs['username_form'], kwargs['password_form']

def parse(self, response):
return FormRequest.from_response(
response,
formdata={self.username_form: self.username,
self.password_form: self.password},
callback=super(FormRequestDjangoSpider, self).parse
)