【爬蟲】使用xpath與lxml移除特定標簽


移除標簽的兩種方式

可以用xpath定位

for bad in html.xpath(".//table"):
    bad.getparent().remove(bad)

參考:https://stackoverflow.com/questions/7981840/how-to-remove-an-element-in-lxml

直接刪除相關標簽

在使用xpath獲取指定標簽后,直接刪除。

etree.strip_elements(f, 'r')

參考:https://stackoverflow.com/questions/5418201/how-can-one-replace-an-element-with-text-in-lxml#answer-5420500


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM