From 5a96a16914ad6e4e54a7bd313802891f82a8dc08 Mon Sep 17 00:00:00 2001 From: Dharmesh Pandav Date: Mon, 6 Apr 2015 14:44:47 +0530 Subject: [PATCH] Update form.py to improve existing capability Add capability to search HTML Form using formid when using `FormRequest.from_response()` refrenced issue :https://github.com/scrapy/scrapy/issues/1136 --- scrapy/http/request/form.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/scrapy/http/request/form.py b/scrapy/http/request/form.py index a4695f1a2..cae56f229 100644 --- a/scrapy/http/request/form.py +++ b/scrapy/http/request/form.py @@ -31,10 +31,10 @@ class FormRequest(Request): self._set_url(self.url + ('&' if '?' in self.url else '?') + querystr) @classmethod - def from_response(cls, response, formname=None, formnumber=0, formdata=None, + def from_response(cls, response, formname=None, formid=None, formnumber=0, formdata=None, clickdata=None, dont_click=False, formxpath=None, **kwargs): kwargs.setdefault('encoding', response.encoding) - form = _get_form(response, formname, formnumber, formxpath) + form = _get_form(response, formname, formid, formnumber, formxpath) formdata = _get_inputs(form, formdata, dont_click, clickdata, response) url = _get_form_url(form, kwargs.pop('url', None)) method = kwargs.pop('method', form.method) @@ -67,6 +67,11 @@ def _get_form(response, formname, formnumber, formxpath): if f: return f[0] + if formid is not None: + f = root.xpath('//form[@id="%s"]' % formid) + if f: + return f[0] + # Get form element from xpath, if not found, go up if formxpath is not None: nodes = root.xpath(formxpath)