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. docker-compose 为何找不到配置文件?2. docker绑定了nginx端口 外部访问不到3. dockerfile - [docker build image失败- npm install]4. docker网络端口映射,没有方便点的操作方法么?5. boot2docker无法启动6. Docker for Mac 创建的dnsmasq容器连不上/不工作的问题7. node.js - cnpm安装electron报错please remove node_modules before retry!。8. angular.js使用$resource服务把数据存入mongodb的问题。9. html5 - 如何让H5页面在手机浏览器里和微信全屏显示10. ruby - 关于rails中devise登录用户名的调用问题

网公网安备