代码
# -*- coding: utf-8 -*-
# @Author : markadc
def del_attr(org_html: str) -> str:
tree = etree.HTML(org_html)
# 1、先获取到 包含删除的属性 的元素目标
elems = tree.xpath('//*[@style or @id or @class or @href or @alt or @data-test]')
# 2、逐一删除 元素属性
for elem in elems:
etree.strip_attributes(elem, ["style", "id", "class", "href", "alt", "data-test"])
# 3、得到 删除属性后 的HTML
new_html = etree.tostring(tree).decode().strip()
return new_html