Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

added new test case for xpathselector_iternodes

Pablo Hoffman49175d199024
diff --git a/scrapy/trunk/scrapy/tests/test_utils_xml.py b/scrapy/trunk/scrapy/tests/test_utils_xml.py
index d12bffd99..1167c3667 100644
--- a/scrapy/trunk/scrapy/tests/test_utils_xml.py
+++ b/scrapy/trunk/scrapy/tests/test_utils_xml.py
@@ -34,6 +34,11 @@ class UtilsXmlTestCase(unittest.TestCase):
self.assertEqual([x.x("@id").extract() for x in xpathselector_iternodes(response, 'product')],
[['34017532'], ['34017557'], ['34017563'], ['34018057'], ['34018313'], ['34018599']])
+ def test_iterator_text(self):
+ body = u"""<?xml version="1.0" encoding="UTF-8"?><products><product>one</product><product>two</product></products>"""
+
+ self.assertEqual([x.x("text()").extract() for x in xpathselector_iternodes(body, 'product')],
+ [[u'one'], [u'two']])
if __name__ == "__main__":
unittest.main()
diff --git a/scrapy/trunk/scrapy/utils/xml.py b/scrapy/trunk/scrapy/utils/xml.py
index 2d43c8b3a..acfa52363 100644
--- a/scrapy/trunk/scrapy/utils/xml.py
+++ b/scrapy/trunk/scrapy/utils/xml.py
@@ -19,6 +19,8 @@ def xpathselector_iternodes(obj, nodename):
text = obj.body.to_unicode()
elif isinstance(obj, str):
text = obj.decode('utf-8')
+ else:
+ text = obj
r = re.compile(r"<%s[\s>].*?</%s>" % (nodename, nodename), re.DOTALL)
for match in r.finditer(text):