【爬虫】使用xpath与lxml移除特定标签

你。 提交于 2020-08-17 18:42:27

移除标签的两种方式

可以用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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!