我想在Calibre中写一个插件,检查一个epub文档中的脚注(基本上是寻找字体大小<;一个特定的值)。<body>
标签)中包含文本,但我遇到了一个问题。
LXML xpath 找不到包含文字的 <body>
或其中的任何东西。
下面是由Calibre自己的函数创建的html和插入的 <p>Hello World</p>
使用 etree.SubElement
<?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)
所以问题是我到底做错了什么!
看来,你遇到了命名空间的问题。有几种方法可以处理这个问题。两个简单的方法是删除命名空间的引用,从
<html xmlns="http://www.w3.org/1999/xhtml">
因此,标签只是 <html>
.
另一种方法是将你的查询改为
//*[local-name()="body"]
看看这些是否有效。