Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

added dont_click attr to FormRequest

Ismael Carnalesa244d23b89f3
diff --git a/docs/topics/request-response.rst b/docs/topics/request-response.rst
index ec5ca5f31..fc52114f7 100644
--- a/docs/topics/request-response.rst
+++ b/docs/topics/request-response.rst
@@ -250,7 +250,7 @@ objects.
The :class:`FormRequest` objects support the following class method in
addition to the standard :class:`Request` methods:
- .. classmethod:: FormRequest.from_response(response, [formnumber=0, formdata=None, clickdata=None, ...])
+ .. classmethod:: FormRequest.from_response(response, [formnumber=0, formdata=None, clickdata=None, dont_click=False, ...])
Returns a new :class:`FormRequest` object with its form field values
pre-populated with those found in the HTML ``<form>`` element contained
@@ -276,6 +276,10 @@ objects.
more info.
:type clickdata: dict
+ :param dont_click: If True the form data will be sumbitted without
+ clicking in any element.
+ :type clickdata: bool
+
The other parameters of this class method are passed directly to the
:class:`FormRequest` constructor.
diff --git a/scrapy/http/request/form.py b/scrapy/http/request/form.py
index 15c383210..e5fefe880 100644
--- a/scrapy/http/request/form.py
+++ b/scrapy/http/request/form.py
@@ -37,7 +37,8 @@ class FormRequest(Request):
self.headers['Content-Type'] = 'application/x-www-form-urlencoded'
@classmethod
- def from_response(cls, response, formnumber=0, formdata=None, clickdata=None, **kwargs):
+ def from_response(cls, response, formnumber=0, formdata=None,
+ clickdata=None, dont_click=False, **kwargs):
encoding = getattr(response, 'encoding', 'utf-8')
forms = ParseFile(StringIO(response.body), response.url,
encoding=encoding, backwards_compat=False)
@@ -55,6 +56,10 @@ class FormRequest(Request):
for k, v in formdata.iteritems():
for v2 in v if hasattr(v, '__iter__') else [v]:
form.new_control('text', k, {'value': v2})
+
+ if not dont_click:
+ url, body, headers = form.click_request_data(**(clickdata or {}))
+ else:
+ url, body, headers = form._switch_click('request_data')
- url, body, headers = form.click_request_data(**(clickdata or {}))
return cls(url, method=form.method, body=body, headers=headers, **kwargs)
diff --git a/scrapy/tests/test_http_request.py b/scrapy/tests/test_http_request.py
index 7e3c6262f..08fe70edf 100644
--- a/scrapy/tests/test_http_request.py
+++ b/scrapy/tests/test_http_request.py
@@ -292,6 +292,21 @@ class FormRequestTest(RequestTest):
self.assertEqual(urlargs['one'], ['1'])
self.assertEqual(urlargs['two'], ['2'])
+ def test_from_response_dont_click(self):
+ respbody = """
+<form action="get.php" method="GET">
+<input type="submit" name="clickeable1" value="clicked1">
+<input type="hidden" name="one" value="1">
+<input type="hidden" name="two" value="3">
+<input type="submit" name="clickeable2" value="clicked2">
+</form>
+ """
+ response = Response("http://www.example.com/this/list.html", body=respbody)
+ r1 = self.request_class.from_response(response, dont_click=True)
+ urlargs = cgi.parse_qs(urlparse(r1.url).query)
+ self.assertFalse('clickeable1' in urlargs, urlargs)
+ self.assertFalse('clickeable2' in urlargs, urlargs)
+
def test_from_response_errors_noform(self):
respbody = """<html></html>"""
response = Response("http://www.example.com/lala.html", body=respbody)