Skip to content

Commit b830e95

Browse files
committed
do not fail handling unicode xpaths in libxml2 backed selectors
1 parent bf3c9ee commit b830e95

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

scrapy/selector/libxml2sel.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def __init__(self, response=None, text=None, node=None, parent=None, expr=None):
3737
def select(self, xpath):
3838
if hasattr(self.xmlNode, 'xpathEval'):
3939
self.doc.xpathContext.setContextNode(self.xmlNode)
40+
xpath = unicode_to_str(xpath, 'utf-8')
4041
try:
4142
xpath_result = self.doc.xpathContext.xpathEval(xpath)
4243
except libxml2.xpathError:

scrapy/tests/test_selector.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ def test_selector_simple(self):
4343
self.assertEqual([x.extract() for x in xpath.select("concat(//input[@name='a']/@value, //input[@name='b']/@value)")],
4444
[u'12'])
4545

46+
def test_selector_unicode_query(self):
47+
body = u"<p><input name='\xa9' value='1'/></p>"
48+
response = TextResponse(url="http://example.com", body=body, encoding='utf8')
49+
xpath = self.hxs_cls(response)
50+
self.assertEqual(xpath.select(u'//input[@name="\xa9"]/@value').extract(), [u'1'])
51+
4652
@libxml2debug
4753
def test_selector_same_type(self):
4854
"""Test XPathSelector returning the same type in x() method"""

0 commit comments

Comments
 (0)