Scrapy xpath used in Chinese error

Problem Description

links = sel.xpath('//i[contains(@title,"置顶")]/following-sibling::a/@href').extract()

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

 

 

Solution

Method a: xpath entire sentence into Unicode

links = sel.xpath(u'//i[contains(@title,"置顶")]/following-sibling::a/@href').extract()

Method Two: xpath statement has been converted to Unicode with the title variable

title = u"置顶"
links = sel.xpath('//i[contains(@title,"%s")]/following-sibling::a/@href' %(title)).extract()

Method three: variable syntax (directly in xpath $Symbol Plus variable names) $title, parameter passing to title

 
links = sel.xpath('//i[contains(@title,$title)]/following-sibling::a/@href', title="置顶").extract()

Guess you like

Origin www.cnblogs.com/jessicor/p/12056144.html