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 - 各位电脑上有多少个容器啊?容器一多,自己都搞混了,咋办呢?2. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””3. docker gitlab 如何git clone?4. docker镜像push报错5. 数据库 - mysql中怎样修改带点的字段名的位置?6. vim - docker中新的ubuntu12.04镜像,运行vi提示,找不到命名.7. golang - 用IDE看docker源码时的小问题8. dockerfile - [docker build image失败- npm install]9. docker不显示端口映射呢?10. docker绑定了nginx端口 外部访问不到

网公网安备