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. mysql 查詢(xún)身份證號(hào)字段值有效的數(shù)據(jù)2. python bottle跑起來(lái)以后,定時(shí)執(zhí)行的任務(wù)為什么每次都重復(fù)(多)執(zhí)行一次?3. 視頻文件不能播放,怎么辦?4. html5 - HTML代碼中的文字亂碼是怎么回事?5. python - 爬蟲(chóng)模擬登錄后,爬取csdn后臺(tái)文章列表遇到的問(wèn)題6. visual-studio - Python OpenCV: 奇怪的自動(dòng)補(bǔ)全問(wèn)題7. mysql - 分庫(kù)分表、分區(qū)、讀寫(xiě)分離 這些都是用在什么場(chǎng)景下 ,會(huì)帶來(lái)哪些效率或者其他方面的好處8. javascript - 彈出一個(gè)子窗口,操作之后關(guān)閉,主窗口會(huì)得到相應(yīng)的響應(yīng),例如網(wǎng)站的某些登錄界面,django后臺(tái)的管理等,這是怎么實(shí)現(xiàn)的呢?9. javascript - ios返回不執(zhí)行js怎么解決?10. android - 分享到微信,如何快速轉(zhuǎn)換成字節(jié)數(shù)組
