Python 3.4+
使用html.unescape()
:
import html
print(html.unescape('£682m'))
FYI html.parser.HTMLParser.unescape
已弃用,was supposed to be removed in 3.5已弃用,尽管它被错误地留在了.它将很快从语言中删除.
Python 2.6-3.3
您可以使用标准库中的HTMLParser.unescape()
:
>>> 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