亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

刪除某個子節點后的子節點

刪除某個子節點后的子節點

胡說叔叔 2021-07-18 20:19:55
我想刪除<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)但它不適用于文本節點。最好的方法是什么?謝謝。
查看完整描述

1 回答

?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

嘗試使用以下代碼刪除節點:


from lxml import etree, html


source = """<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>"""

html = html.fromstring(source)

parent = html.xpath('//td')[0]

redundant = html.xpath('//hr/preceding-sibling::*[1]/following-sibling::*')


for node in redundant:

    parent.remove(node)


print(etree.tostring(parent))

輸出


<td class="one">

    Some text

    <a href="page1.html"/>

    <br/>

    Some more text

    <br/>

    <a href="page2.html"/>

</td>


查看完整回答
反對 回復 2021-07-27
  • 1 回答
  • 0 關注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號