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. javascript - js判断一个数组是否重复2. Docker for Mac 创建的dnsmasq容器连不上/不工作的问题3. java - tomcat服务经常晚上会挂,求解?4. angular.js - angularjs的自定义过滤器如何给文字加颜色?5. node.js - node express 中ajax post请求参数接收不到?6. javascript - 如何获取未来元素的父元素在页面中所有相同元素中是第几个?7. 关于docker下的nginx压力测试8. java - 原生CGLib内部方法互相调用时可以代理,但基于CGLib的Spring AOP却代理失效,为什么?9. docker镜像push报错10. linux运维 - python远程控制windows如何实现
