从纯文本获取 Xpath

Getting Xpath from plain text

我正在尝试从文本而不是 URL 获取 xpath。但我一直收到错误 "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已经15年了,但据我所知,它是一门区分大小写的语言,xpath方法全部是小写

所以试试这个:

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