python - Scrapy中xpath用到中文報(bào)錯(cuò)
問題描述
問題描述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
問題解答
回答1:參見文章:解決Scrapy中xpath用到中文報(bào)錯(cuò)問題
解決方法方法一:將整個(gè)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中變量語法($符號(hào)加變量名)$title, 傳參title即可
links = sel.xpath(’//i[contains(@title,$title)]/following-sibling::a/@href’,).extract()回答2:
整個(gè)字符串前加個(gè)u試試
相關(guān)文章:
1. javascript - h5 video層級(jí)太高導(dǎo)致浮在div上面,如何解決?2. css - 請(qǐng)問B站頂部的模糊半透明導(dǎo)航條是怎么實(shí)現(xiàn)的呢?3. python - pyspider爬pdf爬了一小段時(shí)間后就不動(dòng)了4. linux - Ubuntu下編譯Vim8(+python)無數(shù)次編譯失敗5. mysql ER_BAD_DB_ERROR: Unknown database ’test’6. python中怎么對(duì)列表以區(qū)間進(jìn)行統(tǒng)計(jì)?7. javascript - Ajax加載Json時(shí),移動(dòng)端頁面向左上角縮小一截兒,加載完成后才正常顯示,這該如何解決?8. 如何合并兩張具有相同結(jié)構(gòu)的mysql表9. mysql - 記得以前在哪里看過一個(gè)估算時(shí)間的網(wǎng)站10. python運(yùn)行后沒有任何反饋要怎么排查
