报错信息:
Traceback (most recent call last):
File “c:\Users\86130\Desktop\Study\python\spider_learning\hello_spider.py”, line 6, in
text = ht.xpath(xpath)
File “src\lxml\etree.pyx”, line 1583, in lxml.etree._Element.xpath
File “src\lxml\xpath.pxi”, line 305, in lxml.etree.XPathElementEvaluator.call
File “src\lxml\xpath.pxi”, line 225, in lxml.etree._XPathEvaluatorBase._handle_result
lxml.etree.XPathEvalError: Invalid expression
源代码:
import lxml.html,requests
url = 'https://www.python.org/dev/peps/pep-0020/'
xpath = '//*[@id="the-zen-of-python"]/pre/text()'
response = requests.get(url)
ht = lxml.html.fromstring(response.text)
text = ht.xpath(xpath)
print('hello,\n'+' '.join(text))
发现是xpath路径中含中文字符
在执行Python爬虫时遇到了`lxml.etree.XPathEvalError: Invalid expression`的错误,该错误出现在尝试使用XPath表达式解析HTML内容时。错误位于文件`hello_spider.py`的第6行,通过`ht.xpath(xpath)`执行。要解决这个问题,需要检查XPath表达式是否正确,并确保它符合XPath语法。
665

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



