mirror of https://github.com/scrapy/scrapy.git
fix `tail` issue when extracting nodes [lxml]
This commit is contained in:
parent
43028876b5
commit
045ff8e5a7
|
|
@ -59,7 +59,7 @@ class XPathSelector(object_ref):
|
||||||
def extract(self):
|
def extract(self):
|
||||||
try:
|
try:
|
||||||
return etree.tostring(self._root, method=self._tostring_method, \
|
return etree.tostring(self._root, method=self._tostring_method, \
|
||||||
encoding=unicode)
|
encoding=unicode, with_tail=False)
|
||||||
except (AttributeError, TypeError):
|
except (AttributeError, TypeError):
|
||||||
if self._root is True:
|
if self._root is True:
|
||||||
return u'1'
|
return u'1'
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,7 @@ class XPathSelectorTestCase(unittest.TestCase):
|
||||||
self.assertEqual([x.extract() for x in xpath.select("concat(//input[@name='a']/@value, //input[@name='b']/@value)")],
|
self.assertEqual([x.extract() for x in xpath.select("concat(//input[@name='a']/@value, //input[@name='b']/@value)")],
|
||||||
[u'12'])
|
[u'12'])
|
||||||
|
|
||||||
|
@libxml2debug
|
||||||
def test_selector_unicode_query(self):
|
def test_selector_unicode_query(self):
|
||||||
body = u"<p><input name='\xa9' value='1'/></p>"
|
body = u"<p><input name='\xa9' value='1'/></p>"
|
||||||
response = TextResponse(url="http://example.com", body=body, encoding='utf8')
|
response = TextResponse(url="http://example.com", body=body, encoding='utf8')
|
||||||
|
|
@ -271,7 +272,6 @@ class XPathSelectorTestCase(unittest.TestCase):
|
||||||
|
|
||||||
@libxml2debug
|
@libxml2debug
|
||||||
def test_select_on_text_nodes(self):
|
def test_select_on_text_nodes(self):
|
||||||
# FIXME: can't get this to work with lxml backend
|
|
||||||
r = self.hxs_cls(text=u'<div><b>Options:</b>opt1</div><div><b>Other</b>opt2</div>')
|
r = self.hxs_cls(text=u'<div><b>Options:</b>opt1</div><div><b>Other</b>opt2</div>')
|
||||||
x1 = r.select("//div/descendant::text()[preceding-sibling::b[contains(text(), 'Options')]]")
|
x1 = r.select("//div/descendant::text()[preceding-sibling::b[contains(text(), 'Options')]]")
|
||||||
self.assertEquals(x1.extract(), [u'opt1'])
|
self.assertEquals(x1.extract(), [u'opt1'])
|
||||||
|
|
@ -279,10 +279,15 @@ class XPathSelectorTestCase(unittest.TestCase):
|
||||||
x1 = r.select("//div/descendant::text()/preceding-sibling::b[contains(text(), 'Options')]")
|
x1 = r.select("//div/descendant::text()/preceding-sibling::b[contains(text(), 'Options')]")
|
||||||
self.assertEquals(x1.extract(), [u'<b>Options:</b>'])
|
self.assertEquals(x1.extract(), [u'<b>Options:</b>'])
|
||||||
|
|
||||||
|
@libxml2debug
|
||||||
|
def test_nested_select_on_text_nodes(self):
|
||||||
|
# FIXME: does not work with lxml backend [upstream]
|
||||||
|
r = self.hxs_cls(text=u'<div><b>Options:</b>opt1</div><div><b>Other</b>opt2</div>')
|
||||||
x1 = r.select("//div/descendant::text()")
|
x1 = r.select("//div/descendant::text()")
|
||||||
x2 = x1.select("./preceding-sibling::b[contains(text(), 'Options')]")
|
x2 = x1.select("./preceding-sibling::b[contains(text(), 'Options')]")
|
||||||
|
|
||||||
self.assertEquals(x2.extract(), [u'<b>Options:</b>'])
|
self.assertEquals(x2.extract(), [u'<b>Options:</b>'])
|
||||||
test_select_on_text_nodes.skip = True
|
test_nested_select_on_text_nodes.skip = True
|
||||||
|
|
||||||
@libxml2debug
|
@libxml2debug
|
||||||
def test_weakref_slots(self):
|
def test_weakref_slots(self):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue