我想删除<hr/>元素(包括<hr/>)下方的元素内的所有节点(包括文本)。
例如,这个:
<td class="one">
Some text
<a href="page1.html"/>
<br/>
Some more text
<br/>
<a href="page2.html"/>
<hr/>
Bottom text
<br/>
<a href="page3.html"/>
</td>
应该变成:
<td class="one">
Some text
<a href="page1.html"/>
<br/>
Some more text
<br/>
<a href="page2.html"/>
</td>
我有这个 XPath 来查找下面的所有元素<hr/>:
./node()[ preceding-sibling::hr[not(following-sibling::hr)] ]
但我不知道如何删除这些元素。我试图这样做:
xp = './node()[ preceding-sibling::hr[not(following-sibling::hr)] ]'
els = self.xpath(xp, td_el)
for el in els:
el.getparent().remove(el)
但它不适用于文本节点。
最好的方法是什么?谢谢。
阿晨1998
相关分类