分享技术,记录生活,在互联网上留下属于自己的一亩三分地。

python xpath获取文本报错lxml.etree.XPathEvalError: Invalid expression

默认笔记 novadmin 3545℃ 0评论

代码如下:


from lxml import etree

html = etree.parse('./test.html', etree.HTMLParser())
result = html.xpath('//li[@class="item-0"/text()]')
print(result)

报错:


E:\Download\venv\Scripts\python.exe E:/Download/urllib_xpath_test66.py
Traceback (most recent call last):
  File "E:/Download/urllib_xpath_test66.py", line 5, in <module>
    result = html.xpath('//li[@class="item-0"/text()]')
  File "src\lxml\etree.pyx", line 2278, in lxml.etree._ElementTree.xpath
  File "src\lxml\xpath.pxi", line 359, in lxml.etree.XPathDocumentEvaluator.__call__
  File "src\lxml\xpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._handle_result
lxml.etree.XPathEvalError: Invalid expression

仔细检查,发现应该是如下:


from lxml import etree

html = etree.parse('./test.html', etree.HTMLParser())
result = html.xpath('//li[@class="item-0"]/text()')
print(result)

 

转载请注明:Nov » python xpath获取文本报错lxml.etree.XPathEvalError: Invalid expression

喜欢 (0)or分享 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址