久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

python - Scrapy中xpath用到中文報錯

瀏覽:156日期: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 編程
相關文章:
主站蜘蛛池模板: 泸溪县| 普兰店市| 依安县| 来安县| 定日县| 东乡族自治县| 遂宁市| 深泽县| 巴马| 道孚县| 惠东县| 平顶山市| 西丰县| 西乡县| 荣成市| 仪陇县| 敦煌市| 永春县| 民勤县| 汉沽区| 金溪县| 桃源县| 库车县| 凌源市| 黄山市| 铁岭县| 桦南县| 诸暨市| 博爱县| 红安县| 宜阳县| 宜都市| 仙居县| 涞源县| 延边| 惠州市| 汾阳市| 澄城县| 汝阳县| 肃南| 旌德县|