【发布时间】:2020-05-13 15:40:04
【问题描述】:
我正在尝试在 Calibre 中编写一个插件,用于检查 epub 文档中的脚注(基本上是寻找 font-size <body> 标签内)中获取所有子标签,但我遇到了一个问题。
LXML xpath 找不到 <body> 或其中的任何内容。
下面是使用 Calibre 自己的函数创建的 html 和使用 etree.SubElement 插入的 <p>Hello World</p>
<?xml version='1.0' encoding='utf-8'?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hero filtered</title>
<link href="page_styles.css" rel="stylesheet" type="text/css"/>
<link href="stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<p>Hello World</p></body>
</html>
这些都是我尝试过的东西
query = ".//body" # This doesn't
query = "body" # This doesn't
query = ".//*/body" # This doesn't
query = ".//*//body" # This doesn't
query = "./body" # This doesn't
query = ".//body/*" # This doesn't
query = ".//body/p" # This doesn't
这些确实有效
query = "/*/*[2]/*[normalize-space(text())]" # this works
found= self.footnotes_file.find("{*}" + "body") # this works
我一直在使用 lxml 中的以下函数
found = self.footnotes_file.xpath(query)
self.footnotes_file 是使用 Calibre 函数 parsed(self, name) 生成的,该函数返回传递给它的 html 文件的根元素
self.footnotes_file = current_container().parsed(footnote_file_name)
所以问题是我做错了什么!
【问题讨论】: