从纯文本中获取X路径

问题描述 投票:0回答:1

我试图从文本中获取xpath而不是URL。 但我一直得到错误 "AttributeError: 'HtmlElement' object has no attribute 'XPath'"。

见下面的代码。

from lxml import html

var ='''<html lang="en">
  <head>
    <title>Selecting content on a web page with XPath</title>
  </head>
  <body>
     This is the body
  </body>
</html>
'''

tree = html.fromstring(var)

body = tree.XPath('//*/body')

print(body)

python xpath lxml
1个回答
1
投票

我上一次使用Python已经有15年了,但据我所知,它是一种区分大小写的语言,而且它的 xpath 方法都是小写的。

所以试试这个。

body = tree.xpath('//*/body')
© www.soinside.com 2019 - 2024. All rights reserved.