<p>考虑一下这个Python脚本:</p>
<pre><code>from lxml import etree
html = '''
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<p>This is some text followed with 2 citations.<span class="footnote">1</span>
<span сlass="footnote">2</span>This is some more text.</p>
</body>
</html>'''
tree = etree.fromstring(html)
for element in tree.findall(".//{*}span"):
if element.get("class") == 'footnote':
print(etree.tostring(element, encoding="unicode", pretty_print=True))
</code></pre>
<p>期望的输出是2<code>span</code>元素,而我得到:</p>
^{pr2}$
<p>为什么它在元素后面包含文本直到父元素的结尾?在</p>
<p>我试图使用lxml链接脚注,当我将<code>a.insert()</code>元素添加到我为其创建的<code>a</code>元素中时,它包含了后面的文本,因此链接了大量我不想链接的文本。在</p>