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

猜你喜欢

转载自www.cnblogs.com/XD00/p/10626312.html