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 - .vue页面跳转问题2. java - 微信小程序中 无缘无故的提示(图片的加载失败)3. [python2]local variable referenced before assignment问题4. python - TypeError: tryMsgcode() takes exactly 2 arguments (0 given)5. javascript - 像这种内联Js绑定方式,如何定位到js代码?6. python - 一个程序中的切片问题7. javascript - vue-cli中 用proxyTable实现了跨域,用get访问没有问题,但通过 post传数据就出现了问题8. javascript - 求一款靠谱点的移动端图片查看器插件,老司机速进!9. javascript - 使用form进行页面跳转,但是很慢,如何加一个Loading?10. module - python模块from import语句相对导入,加不加点号有什么区别?

网公网安备