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. css - 求推薦幾款好用的移動端頁面布局調試工具呢?2. javascript - 百度echarts series數據更新問題3. css3 - css before 中文亂碼?4. php - 第三方支付平臺在很短時間內多次異步通知,訂單多次確認收款5. Mysql && Redis 并發問題6. javascript - node服務端渲染的困惑7. javascript - 請問一下組件的生命周期beforeDestory是在什么情況下面觸發的呢?8. mysql - 一個表和多個表是多對多的關系,該怎么設計9. python - type函數問題10. mysql新建字段時 timestamp NOT NULL DEFAULT ’0000-00-00 00:00:00’ 報錯
