1
0
mirror of https://github.com/scrapy/scrapy.git synced 2025-02-23 10:04:07 +00:00

split nested_loader into seperate methods

This commit is contained in:
Daniel Collins 2015-09-04 12:19:10 -07:00
parent 88c92cb68b
commit 311d5cd495
2 changed files with 20 additions and 24 deletions

View File

@ -50,18 +50,19 @@ class ItemLoader(object):
else:
return self._local_item
def nested_loader(self, xpath=None, css=None):
if xpath is not None and css is not None:
raise ValueError("Cannot nest a loader with both a xpath selector and a css selector")
if xpath is not None:
selector = self.selector.xpath(xpath)
if css is not None:
selector = self.selector.css(css)
def nested_xpath(self, xpath, **context):
selector = self.selector.xpath(xpath)
context.update(selector=selector)
subloader = self.__class__(
item=self.item, selector=selector, parent=self
item=self.item, parent=self, **context
)
return subloader
def nested_css(self, css, **context):
selector = self.selector.css(css)
context.update(selector=selector)
subloader = self.__class__(
item=self.item, parent=self, **context
)
return subloader

View File

@ -631,7 +631,7 @@ class SubselectorLoaderTest(unittest.TestCase):
def test_nested_xpath(self):
l = NestedItemLoader(response=self.response)
nl = l.nested_loader(xpath="//header")
nl = l.nested_xpath("//header")
nl.add_xpath('name', 'div/text()')
nl.add_css('name_div', '#id')
nl.add_value('name_value', nl.selector.xpath('div[@id = "id"]/text()').extract())
@ -646,7 +646,7 @@ class SubselectorLoaderTest(unittest.TestCase):
def test_nested_css(self):
l = NestedItemLoader(response=self.response)
nl = l.nested_loader(css="header")
nl = l.nested_css("header")
nl.add_xpath('name', 'div/text()')
nl.add_css('name_div', '#id')
nl.add_value('name_value', nl.selector.xpath('div[@id = "id"]/text()').extract())
@ -661,8 +661,8 @@ class SubselectorLoaderTest(unittest.TestCase):
def test_nested_replace(self):
l = NestedItemLoader(response=self.response)
nl1 = l.nested_loader(xpath='//footer')
nl2 = nl1.nested_loader(xpath='a')
nl1 = l.nested_xpath('//footer')
nl2 = nl1.nested_xpath('a')
l.add_xpath('url', '//footer/a/@href')
self.assertEqual(l.get_output_value('url'), [u'http://www.scrapy.org'])
@ -673,8 +673,8 @@ class SubselectorLoaderTest(unittest.TestCase):
def test_nested_ordering(self):
l = NestedItemLoader(response=self.response)
nl1 = l.nested_loader(xpath='//footer')
nl2 = nl1.nested_loader(xpath='a')
nl1 = l.nested_xpath('//footer')
nl2 = nl1.nested_xpath('a')
nl1.add_xpath('url', 'img/@src')
l.add_xpath('url', '//footer/a/@href')
@ -690,8 +690,8 @@ class SubselectorLoaderTest(unittest.TestCase):
def test_nested_load_item(self):
l = NestedItemLoader(response=self.response)
nl1 = l.nested_loader(xpath='//footer')
nl2 = nl1.nested_loader(xpath='img')
nl1 = l.nested_xpath('//footer')
nl2 = nl1.nested_xpath('img')
l.add_xpath('name', '//header/div/text()')
nl1.add_xpath('url', 'a/@href')
@ -707,11 +707,6 @@ class SubselectorLoaderTest(unittest.TestCase):
self.assertEqual(item['url'], [u'http://www.scrapy.org'])
self.assertEqual(item['image'], [u'/images/logo.png'])
def test_nested_bad_arguments(self):
l = NestedItemLoader(response=self.response)
with self.assertRaises(ValueError):
l.nested_loader(css="#id", xpath="//footer")
class SelectJmesTestCase(unittest.TestCase):
test_list_equals = {