python - Scrapy中xpath用到中文報錯
問題描述
問題描述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語句轉(zhuǎn)成Unicode
links = sel.xpath(u’//i[contains(@title,'置頂')]/following-sibling::a/@href’).extract()
方法二:xpath語句用已轉(zhuǎn)成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試試
相關(guān)文章:
1. javascript - immutable配合react提升性能?2. javascript - sublime快鍵鍵問題3. javascript - nodejs關(guān)于進程間發(fā)送句柄的一點疑問4. javascript - 移動端上不能實現(xiàn)拖拽布局嗎?5. 實現(xiàn)bing搜索工具urlAPI提交6. Apache 已經(jīng)把網(wǎng)站根目錄的改為allow from all了,但是服務(wù)器還是不能訪問?7. css - 寫頁面遇到個布局問題,求大佬們幫解答,在線等,急!~8. phpstudy8.1支持win11系統(tǒng)嗎?9. 配置Apache時,添加對PHP的支持時語法錯誤10. vue.js - Vue 如何像Angular.js watch 一樣監(jiān)聽數(shù)據(jù)變化

網(wǎng)公網(wǎng)安備