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. index.php错误,求指点2. c++ - pycharm 4.5 的 python -> preferences 找不到3. 网页爬虫 - python爬虫,需要爬取的数据没在网页源代码中,怎么处理?4. 关于mysql unique的问题,如图所示5. mysql - 要取出数据库中按某字段排序后的前10,20,30条数据要怎么做?6. showpassword里的this 是什么意思?代表哪个元素7. android百度地图定位问题8. 求大神帮我看看是哪里写错了 感谢细心解答9. javascript - ajax请求不返回,关闭页面时才返回。。。10. javascript - 可以DIY的页面是如何实现的?