python - Scrapy中xpath用到中文報(bào)錯(cuò)
問(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試試
相關(guān)文章:
1. angular.js - 輸入郵箱地址之后, 如何使其自動(dòng)在末尾添加分號(hào)?2. angular.js - $stateChangeSuccess事件在狀態(tài)跳轉(zhuǎn)的時(shí)候不執(zhí)行?3. 如何解決docker宿主機(jī)無(wú)法訪問(wèn)容器中的服務(wù)?4. javascript - 如何使用nodejs 將.html 文件轉(zhuǎn)化成canvas5. python - Scrapy存在內(nèi)存泄漏的問(wèn)題。6. android - rxjava merge 返回Object對(duì)象數(shù)據(jù)如何緩存7. 如何用筆記本上的apache做微信開(kāi)發(fā)的服務(wù)器8. javascript - 螞蟻金服里的react Modal方法,是怎么把元素插入到頁(yè)面最后的9. java如何生成token?10. CSS3 畫(huà)如下圖形
