用Python字符串解码HTML实体?

用Python字符串解码HTML实体?,第1张

用Python字符串解码HTML实体? Python 3.4以上

用途

html.unescape()

import htmlprint(html.unescape('£682m'))

FYI

html.parser.HTMLParser.unescape
已弃用,并且应该在3.5中删除,尽管它是错误地保留的。它将很快从语言中删除。


Python 2.6-3.3

您可以

HTMLParser.unescape()
从标准库中使用:

  • 对于python 2.6-2.7
    HTMLParser
  • 对于Python 3

    html.parser

    try:
    … # Python 2.6-2.7
    … from HTMLParser import HTMLParser
    … except importError:
    … # Python 3
    … from html.parser import HTMLParser

    h = HTMLParser()
    print(h.unescape(‘£682m’))
    £682m

您还可以使用

six
兼容性库来简化导入:

>>> from six.moves.html_parser import HTMLParser>>> h = HTMLParser()>>> print(h.unescape('£682m'))£682m


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5566604.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-14
下一篇2022-12-14

发表评论

登录后才能评论

评论列表(0条)

    保存