From 045ff8e5a7cbd5949f089af0fa613809ef8ef552 Mon Sep 17 00:00:00 2001 From: Alexandru Cepoi Date: Mon, 7 May 2012 17:23:27 +0200 Subject: [PATCH] fix `tail` issue when extracting nodes [lxml] --- scrapy/selector/lxmlsel.py | 2 +- scrapy/tests/test_selector.py | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/scrapy/selector/lxmlsel.py b/scrapy/selector/lxmlsel.py index af6839474..6e5fda556 100644 --- a/scrapy/selector/lxmlsel.py +++ b/scrapy/selector/lxmlsel.py @@ -59,7 +59,7 @@ class XPathSelector(object_ref): def extract(self): try: return etree.tostring(self._root, method=self._tostring_method, \ - encoding=unicode) + encoding=unicode, with_tail=False) except (AttributeError, TypeError): if self._root is True: return u'1' diff --git a/scrapy/tests/test_selector.py b/scrapy/tests/test_selector.py index e4197b084..284c28db6 100644 --- a/scrapy/tests/test_selector.py +++ b/scrapy/tests/test_selector.py @@ -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)")], [u'12']) + @libxml2debug def test_selector_unicode_query(self): body = u"

" response = TextResponse(url="http://example.com", body=body, encoding='utf8') @@ -271,7 +272,6 @@ class XPathSelectorTestCase(unittest.TestCase): @libxml2debug def test_select_on_text_nodes(self): - # FIXME: can't get this to work with lxml backend r = self.hxs_cls(text=u'
Options:opt1
Otheropt2
') x1 = r.select("//div/descendant::text()[preceding-sibling::b[contains(text(), 'Options')]]") 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')]") self.assertEquals(x1.extract(), [u'Options:']) + @libxml2debug + def test_nested_select_on_text_nodes(self): + # FIXME: does not work with lxml backend [upstream] + r = self.hxs_cls(text=u'
Options:opt1
Otheropt2
') x1 = r.select("//div/descendant::text()") x2 = x1.select("./preceding-sibling::b[contains(text(), 'Options')]") + self.assertEquals(x2.extract(), [u'Options:']) - test_select_on_text_nodes.skip = True + test_nested_select_on_text_nodes.skip = True @libxml2debug def test_weakref_slots(self):