python - Scrapy中xpath用到中文报错
问题描述
问题描述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
问题解答
回答1:参见文章:解决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’,).extract()回答2:
整个字符串前加个u试试
相关文章:
1. angular.js - ng-repeat嵌套的directive link函数未执行2. Help!求帮助3. javascript - 关于一段 for 循环代码执行顺序的问题4. ueditor上传服务器提示后端配置项没有正常加载,求助!!!!!5. javascript - 怎么将指定内容提供js分享到Line上?6. javascript - h5 video层级太高导致浮在div上面,如何解决?7. javascript - 微信“分享到朋友圈”自定义分享内容,无法成功调用jssdk接口,不知为何8. 请问连接文件怎么写9. HTML5禁止img预览该怎么解决?10. 为什么PHP链接sql乱码