Search code examples
xpathscrapycyrillic

Passing Cyrilics in Xpath Contains returns XML value error. Scrapy. Python 2


I am trying to get an element like this by xpath Text contains.

<p><strong>Полное наименование</strong></p>

As a result I am getting this error.

    In [4]: response.xpath("//p[contains(text(),'Полное')]").extract()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-7e122465e645> in <module>()
----> 1 response.xpath("//p[contains(text(),'Полное')]").extract()

c:\python27\lib\site-packages\scrapy\http\response\text.pyc in xpath(self, query, **kwargs)
    117
    118     def xpath(self, query, **kwargs):
--> 119         return self.selector.xpath(query, **kwargs)
    120
    121     def css(self, query):

c:\python27\lib\site-packages\parsel\selector.pyc in xpath(self, query, namespaces, **kwargs)
    226             result = xpathev(query, namespaces=nsp,
    227                              smart_strings=self._lxml_smart_strings,
--> 228                              **kwargs)
    229         except etree.XPathError as exc:
    230             msg = u"XPath error: %s in %s" % (exc, query)

src\lxml\etree.pyx in lxml.etree._Element.xpath()

src\lxml\xpath.pxi in lxml.etree.XPathElementEvaluator.__call__()

src\lxml\apihelpers.pxi in lxml.etree._utf8()

ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters

Here is my xpath

response.xpath("//p[contains(text(),'Полное')]").extract()

'Полное' is russian text I use for search. How do I fix the error?


Solution

  • Prefix your expression string with a u to make a unicode string:

    response.xpath(u"//p[contains(text(),'Полное')]").extract()