From 6b4439eacc5612ce584de94053e725610f2e15a5 Mon Sep 17 00:00:00 2001 From: bosnj Date: Fri, 10 Apr 2015 15:32:32 +0200 Subject: [PATCH 1/2] default return value for extract_first --- scrapy/selector/unified.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scrapy/selector/unified.py b/scrapy/selector/unified.py index c0eefb85e..db8b0bc2d 100644 --- a/scrapy/selector/unified.py +++ b/scrapy/selector/unified.py @@ -182,9 +182,9 @@ class SelectorList(list): def extract(self): return [x.extract() for x in self] - def extract_first(self): + def extract_first(self, default=None): for x in self: - return x.extract() + return x.extract() or default @deprecated(use_instead='.extract()') def extract_unquoted(self): From 8ae05478beea4327f556f21c9c0f954826aff832 Mon Sep 17 00:00:00 2001 From: bosnj Date: Mon, 4 May 2015 21:22:17 +0200 Subject: [PATCH 2/2] added docs and test case, fixed handling empty string vs None --- docs/topics/selectors.rst | 5 +++++ scrapy/selector/unified.py | 4 +++- tests/test_selector.py | 8 ++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/docs/topics/selectors.rst b/docs/topics/selectors.rst index 33958cee5..f8a9b0410 100644 --- a/docs/topics/selectors.rst +++ b/docs/topics/selectors.rst @@ -149,6 +149,11 @@ It returns ``None`` if no element was found: >>> sel.xpath('//div/[id="not-exists"]/text()').extract_first() is None True +A default return value can be provided as an argument, to be used instead of ``None``: + + >>> sel.xpath('//div/[id="not-exists"]/text()').extract_first(default='not-found') + 'not-found' + Notice that CSS selectors can select text or attribute nodes using CSS3 pseudo-elements:: diff --git a/scrapy/selector/unified.py b/scrapy/selector/unified.py index db8b0bc2d..efb51b561 100644 --- a/scrapy/selector/unified.py +++ b/scrapy/selector/unified.py @@ -184,7 +184,9 @@ class SelectorList(list): def extract_first(self, default=None): for x in self: - return x.extract() or default + return x.extract() + else: + return default @deprecated(use_instead='.extract()') def extract_unquoted(self): diff --git a/tests/test_selector.py b/tests/test_selector.py index 9b8613319..985424645 100644 --- a/tests/test_selector.py +++ b/tests/test_selector.py @@ -72,6 +72,14 @@ class SelectorTestCase(unittest.TestCase): self.assertEqual(sel.xpath('/ul/li[@id="doesnt-exist"]/text()').extract_first(), None) + def test_extract_first_default(self): + """Test if extract_first() returns default value when no results found""" + body = '' + response = TextResponse(url="http://example.com", body=body) + sel = self.sscls(response) + + self.assertEqual(sel.xpath('//div/text()').extract_first(default='missing'), 'missing') + def test_re_first(self): """Test if re_first() returns first matched element""" body = ''