您的位置:首頁技術文章
文章詳情頁

python - Scrapy中xpath用到中文報錯

瀏覽:122日期:2022-06-27 08:01:12

問題描述

問題描述

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

問題解答

回答1:

參見文章:解決Scrapy中xpath用到中文報錯問題

解決方法

方法一:將整個xpath語句轉成Unicode

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

方法二:xpath語句用已轉成Unicode的title變量

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

方法三:直接用xpath中變量語法($符號加變量名)$title, 傳參title即可

links = sel.xpath(’//i[contains(@title,$title)]/following-sibling::a/@href’,).extract()回答2:

整個字符串前加個u試試

標簽: Python 編程
相關文章:
国产综合久久一区二区三区