现在我得到了如下的xml:
<div>
<p>the first paragraph</p>
<p>the sencond paragraph</p>
something others...
</div>
我想从对象content
中删除这些其他内容...... 。
我知道可以使用content.xpath('.//text()[not(ancestor::p)]')
来获取它,但似乎不是直接从对象中删除这些文本的好方法。
更新:我试过了//p[last()]/following::*
,它无法正常工作......
答案 0 :(得分:2)
它们存储在上一个兄弟标记的tail
属性中,因此要删除所有这些&#34;其他内容...&#34;做:
for elem in document.iter():
elem.tail = ''
修改强>:
删除文档中每个最后p
个兄弟的尾文:
for elem in document.iter():
if elem.tag == 'p' and not elem.getnext():
elem.tail = ''