links = sel.xpath('//i[contains(@title,"置頂")]/following-sibling::a/@href').extract()
錯(cuò)誤:ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters
光陰似箭催人老,日月如移越少年。
請(qǐng)參考文章:解決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變數(shù)
title = u"置頂"
links = sel.xpath('//i[contains(@title,"%s")]/following-sibling::a/@href' %(title)).extract()
方法三:直接用xpath中變數(shù)語(yǔ)法($
符號(hào)加變數(shù)名)$title
, 傳參title即可
links = sel.xpath('//i[contains(@title,$title)]/following-sibling::a/@href', title="置頂").extract()