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語句轉成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試試
相關文章:
1. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””2. docker-compose中volumes的問題3. boot2docker無法啟動4. docker安裝后出現Cannot connect to the Docker daemon.5. nignx - docker內nginx 80端口被占用6. dockerfile - 為什么docker容器啟動不了?7. node.js - antdesign怎么集合react-redux對input控件進行初始化賦值8. docker容器呢SSH為什么連不通呢?9. java - SSH框架中寫分頁時service層中不能注入分頁類10. 關于docker下的nginx壓力測試

網公網安備