python - Scrapy中xpath用到中文報(bào)錯(cuò)
問題描述
問題描述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
問題解答
回答1:參見文章:解決Scrapy中xpath用到中文報(bào)錯(cuò)問題
解決方法方法一:將整個(gè)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中變量語法($符號(hào)加變量名)$title, 傳參title即可
links = sel.xpath(’//i[contains(@title,$title)]/following-sibling::a/@href’,).extract()回答2:
整個(gè)字符串前加個(gè)u試試
相關(guān)文章:
1. mac里的docker如何命令行開啟呢?2. css3 - progress漸變效果css3. docker綁定了nginx端口 外部訪問不到4. 如何解決docker宿主機(jī)無法訪問容器中的服務(wù)?5. css3 隱藏文本6. angular.js - AngularJS如何添加的DOM元素且能綁定事件7. html5 - svg如何做到一組動(dòng)畫循環(huán)8. vue.js - vue+webpack+vue-router 部署到nginx服務(wù)器下,非根目錄,前后端怎樣配置文件?9. 老師可以把數(shù)據(jù)庫也給我們嗎10. node.js - 在vuejs-templates/webpack中dev-server.js里為什么要exports readyPromise?
