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
学习ing2017-06-30 09:57:44
請參考文章:解決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', title="置顶").extract()