python 如何过滤所有html标签的属性除img a标签的href属性

你可以使用Python中的正则表达式来删除HTML标签中的属性,但考虑到HTML的复杂性,更推荐使用HTML解析库(如Beautiful Soup)来处理。以下是使用正则表达式和Beautiful Soup两种方法分别实现的示例:

使用正则表达式(不推荐,可能不够稳定):


import re

def remove_attributes_except_src_and_href(html_text):
    # 使用正则表达式匹配HTML标签中的属性,但保留img的src和a标签的href属性
    cleaned_html = re.sub(r'<(?!img|a)([^>]+)>', r'<\1>', html_text)
    
    return cleaned_html

# 原始的HTML文本
html_content = '<p style="color: red;">This is a <a href="https://example.com">link</a>.</p> <img src="image.jpg" alt="Image">'

# 删除标签中的属性(除img的src和a标签的href属性)后的HTML文本
filtered_html = remove_attributes_except_src_and_href(html_content)
print(filtered_html) 
使用Beautiful Soup(推荐):


from bs4 import BeautifulSoup

def remove_attributes_except_src_and_href(html_text):
    soup = BeautifulSoup(html_text, 'html.parser')
    
    # 遍历所有标签
    for tag in soup.find_all():
        if tag.name != 'img' and tag.name != 'a':
            tag.attrs = {}
    
    return str(soup)

# 原始的HTML文本
html_content = '<p style="color: red;">This is a <a href="https://example.com">link</a>.</p> <img src="image.jpg" alt="Image">'

# 删除标签中的属性(除img的src和a标签的href属性)后的HTML文本
filtered_html = remove_attributes_except_src_and_href(html_content)
print(filtered_html)

无论使用哪种方法,都请在处理HTML时谨慎考虑各种情况,以确保操作的准确性和稳定性。



(本文内容根据网络资料整理和来自用户投稿,出于传递更多信息之目的,不代表本站其观点和立场。也不对其真实性、可靠性承担任何法律责任,特此声明!)

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部