python中将HTML转义字符转换成相应的符号

import html

# 将HTML转义字符转换成相应的符号
def decode_html_symbols(s):
    # 将">"转换成">"
    s = s.replace(">", ">")
    # 将"&lt;"转换成"<"
    s = s.replace("&lt;", "<")
    # 将"&nbsp;"转换成空格
    s = s.replace("&nbsp;", " ")
    # 将"&hellip;"转换成省略号
    s = s.replace("&hellip;", "…")
    # 将"&ldquo;"和"&rdquo;"转换成左右双引号
    s = s.replace("&ldquo;", "“").replace("&rdquo;", "”")
    # 将"&lsquo;"和"&rsquo;"转换成左右单引号
    s = s.replace("&lsquo;", "‘").replace("&rsquo;", "’")
    # 将"&mdash;"转换成破折号
    s = s.replace("&mdash;", "—")
    # 将"&ndash;"转换成短横线
    s = s.replace("&ndash;", "–")
    # 将"&quot;"转换成双引号
    s = s.replace("&quot;", '"')
    # 将"&apos;"转换成单引号
    s = s.replace("&apos;", "'")
    # 将"&euro;"转换成欧元符号
    s = s.replace("&euro;", "€")
    # 将"&pound;"转换成英镑符号
    s = s.replace("&pound;", "£")
    # 将"&yen;"转换成日元符号
    s = s.replace("&yen;", "¥")
    # 将"&sect;"转换成小节符号
    s = s.replace("&sect;", "§")
    # 将"&para;"转换成段落符号
    s = s.replace("&para;", "¶")
    # 将其他HTML转义字符进行转换
    s = html.unescape(s)
    return s
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值