diff --git a/scrapy/selector/factories.py b/scrapy/selector/factories.py index ec9d2bfe8..44dc4f94f 100644 --- a/scrapy/selector/factories.py +++ b/scrapy/selector/factories.py @@ -24,7 +24,7 @@ def body_as_utf8(response): def xmlDoc_from_html(response): """Return libxml2 doc for HTMLs""" - utf8body = body_as_utf8(response) + utf8body = body_as_utf8(response) or ' ' try: lxdoc = libxml2.htmlReadDoc(utf8body, response.url, 'utf-8', \ html_parser_options) @@ -35,7 +35,7 @@ def xmlDoc_from_html(response): def xmlDoc_from_xml(response): """Return libxml2 doc for XMLs""" - utf8body = body_as_utf8(response) + utf8body = body_as_utf8(response) or ' ' try: lxdoc = libxml2.readDoc(utf8body, response.url, 'utf-8', \ xml_parser_options) diff --git a/scrapy/tests/test_selector.py b/scrapy/tests/test_selector.py index a802da704..8ceb87ed6 100644 --- a/scrapy/tests/test_selector.py +++ b/scrapy/tests/test_selector.py @@ -241,6 +241,12 @@ class XPathSelectorTestCase(unittest.TestCase): u'\n ', u'\n pff\n']) + @libxml2debug + def test_empty_bodies(self): + r1 = TextResponse('http://www.example.com', body='') + hxs = HtmlXPathSelector(r1) # shouldn't raise error + xxs = XmlXPathSelector(r1) # shouldn't raise error + @libxml2debug def test_weakref_slots(self): """Check that classes are using slots and are weak-referenceable"""