Merge pull request #1145 from bosnj/master

[MRG+1] default return value for extract_first
This commit is contained in:
Mikhail Korobov 2015-05-21 22:03:54 +05:00
commit cc2258b2bb
3 changed files with 16 additions and 1 deletions

View File

@ -149,6 +149,11 @@ It returns ``None`` if no element was found:
>>> response.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::

View File

@ -184,9 +184,11 @@ 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()
else:
return default
@deprecated(use_instead='.extract()')
def extract_unquoted(self):

View File

@ -73,6 +73,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 = '<ul><li id="1">1</li><li id="2">2</li></ul>'
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 = '<ul><li id="1">1</li><li id="2">2</li></ul>'