1、问题描述:
实现定位<h2>品牌</h2>节点
brand_tag = sel.xpath("//h2[text()= '品牌']")
报错:ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters
2、解决方法:
(1)brand = u'品牌'
brand_tag = sel.xpath("//h2[text()= '%s']"%(brand))
(2)brand_tag = sel.xpath(u"//h2[text()= '品牌']")

本文介绍了解决XPath定位中遇到Unicode或ASCII编码错误的方法。通过使用Unicode字符串进行匹配,成功定位到了目标元素,避免了NULL字节或控制字符引起的错误。
525

被折叠的 条评论
为什么被折叠?



