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
光阴似箭催人老,日月如移越少年。
請參考文章:解決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即可
$
$title
links = sel.xpath('//i[contains(@title,$title)]/following-sibling::a/@href', title="置顶").extract()
整個字串前加個u試試
請參考文章:解決Scrapy中xpath用到中文報錯問題
解決方法
方法一:將整個xpath語句轉換成Unicode
方法二:xpath語句用已轉換成Unicode的title變數
方法三:直接用xpath中變數語法(
$
符號加變數名)$title
, 傳參title即可整個字串前加個u試試