【爬虫】使用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