python - Scrapy中xpath用到中文報錯
大家讲道理
大家讲道理 2017-06-30 09:55:44

問題描述

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

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回覆(2)
学习ing

参见文章:解决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()
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!