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. 關(guān)docker hub上有些鏡像的tag被標記““This image has vulnerabilities””2. 為什么我ping不通我的docker容器呢???3. docker-compose中volumes的問題4. php工具中的mysql還是5.1以下的,請問如何才能升級到5.1以上?5. angular.js - angular 配置代理proxy.conf.json后報錯,頁面返回500internal server error?6. javascript - 移動端自適應(yīng)7. dockerfile - [docker build image失敗- npm install]8. atom開始輸入!然后按tab只有空格出現(xiàn)沒有html格式出現(xiàn)9. 關(guān)于docker下的nginx壓力測試10. Mysql如何按照日期對比數(shù)據(jù),求SQL語句
