成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python - Scrapy中xpath用到中文報(bào)錯(cuò)

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

問(wèn)題描述

問(wèn)題描述

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

報(bào)錯(cuò):ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters

問(wèn)題解答

回答1:

參見(jiàn)文章:解決Scrapy中xpath用到中文報(bào)錯(cuò)問(wèn)題

解決方法

方法一:將整個(gè)xpath語(yǔ)句轉(zhuǎn)成Unicode

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

方法二:xpath語(yǔ)句用已轉(zhuǎn)成Unicode的title變量

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

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

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

整個(gè)字符串前加個(gè)u試試

標(biāo)簽: Python 編程
相關(guān)文章: