【问题标题】:Convert html entities to ascii in Python在 Python 中将 html 实体转换为 ascii
【发布时间】:2009-07-29 04:00:35
【问题描述】:

我需要使用 Python 将任何 html 实体转换为其 ASCII 等价物。我的用例是我正在清理一些用于构建电子邮件的 HTML,以从 HTML 创建纯文本电子邮件。

现在,我只有在需要 ASCII(我认为)时才真正知道如何从这些实体创建 unicode,以便纯文本电子邮件能够正确读取重音字符等内容。我认为一个基本的例子是 html 实体“& aacute;”或 á 被编码为 ASCII。

此外,我什至不能 100% 确定 ASCII 是我对纯文本电子邮件所需要的。如您所知,我完全迷失在这些编码方面。

【问题讨论】:

    标签: python ascii


    【解决方案1】:

    这是一个完整的实现,它也处理 unicode html 实体。您可能会发现它很有用。

    它返回一个不是ascii的unicode字符串,但是如果你想要纯ascii,你可以修改替换操作,使其将实体替换为空字符串。

    def convert_html_entities(s):
        matches = re.findall("&#\d+;", s)
        if len(matches) > 0:
            hits = set(matches)
            for hit in hits:
                name = hit[2:-1]
                try:
                    entnum = int(name)
                    s = s.replace(hit, unichr(entnum))
                except ValueError:
                    pass
    
        matches = re.findall("&#[xX][0-9a-fA-F]+;", s)
        if len(matches) > 0:
            hits = set(matches)
            for hit in hits:
                hex = hit[3:-1]
                try:
                    entnum = int(hex, 16)
                    s = s.replace(hit, unichr(entnum))
                except ValueError:
                    pass
    
        matches = re.findall("&\w+;", s)
        hits = set(matches)
        amp = "&"
        if amp in hits:
            hits.remove(amp)
        for hit in hits:
            name = hit[1:-1]
            if htmlentitydefs.name2codepoint.has_key(name):
                s = s.replace(hit, unichr(htmlentitydefs.name2codepoint[name]))
        s = s.replace(amp, "&")
        return s 
    

    编辑:添加了十六进制代码的匹配。我已经使用了一段时间了,并且遇到了我的第一个情况, ' 是单引号/撇号。

    【讨论】:

    • 不错的答案。似乎标准模块中应该有一些东西可以做到这一点。
    【解决方案2】:

    ASCII 是美国信息交换标准代码,包含任何重音字母。您最好的选择是获取 Unicode(如您所说)并将其编码为 UTF-8(如果您正在处理严重编码错误的用户代理/客户端,可能是 ISO-8859-1 或一些奇怪的代码页,叹息) - - 该部分的内容类型标头与 text/plain 一起可以表达您选择使用的编码(我建议尝试 UTF-8,除非您已经明确证明它无法工作 - 它几乎这些天来普遍支持,比任何 ISO-8859 或“代码页”黑客更灵活!)。

    【讨论】:

    • 至少在我的初步测试中,这很好地将东西先转移到 unicode,然后再转移到 UTF-8。我们明天必须发送一些邮件,看看它在电子邮件客户端中的表现如何。感谢您详细解释我可能真正想要的东西。
    【解决方案3】:

    您可以使用htmlentitydefs 包:

    import htmlentitydefs
    print htmlentitydefs.entitydefs['aacute']
    

    基本上,entitydefs 只是一个字典,您可以通过在 python 提示符下打印它来查看:

    from pprint import pprint 
    pprint htmlentitydefs.entitydefs
    

    【讨论】:

    • 为您提供(大致)ISO-8859-1 代码——即使对于顽固的“西方/欧洲至上主义者”(例如,即使是 euro标志 不适合那里...!!!)。 htmlentitydefs.name2codepoint,它统一为您提供数字代码点(您可以使用 unichr 将其转换为长度为 1 的 unicode 字符串,然后根据需要进行 .encode),这是非常可取的。
    • 感谢您的输入。它似乎确实让我得到了我要求的 ASCII 式的东西,但我还需要更多关于我的用例的指导。
    • @aezell:当然。我也更喜欢亚历克斯的回答,从大局来看,以及他对我自己回答的具体建议。 :)
    【解决方案4】:

    我们搭建了一个带有agazso功能的小模块:

    http://github.com/ARTFL/util/blob/master/ents.py

    我们发现 agazso 的功能比 ent 转换的替代品更快。感谢您发布它。

    【讨论】:

      猜你喜欢
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-13
      • 1970-01-01
      • 2016-03-10
      • 1970-01-01
      • 2010-10-18
      相关资源
      最近更新 更多