Python Glossary Русский

Python 3.4+Strings (str)function

html.unescape()

HTML escaping

Turns HTML entities (<, &, ©, ©, A) back into characters — the inverse of html.escape.

Syntax

html.unescape(s)

Parsing follows the HTML5 rules rather than "up to the semicolon": legacy entities such as &amp and &not are recognised without a ;, so the string '&notit;' becomes '¬it;'. The function knows all 2000+ named entities, including   — it becomes the non-breaking space '\xa0', which is not equal to ' ', so comparisons with a plain space fail. To parse a whole page use html.parser, not unescape.

Examples

import html
print(html.unescape('&lt;b&gt; &copy; &#169; &#x41;'))  # → <b> © © A
print(html.unescape('&notit;'))  # → ¬it; — &not узнан без точки с запятой
print(html.unescape('a&nbsp;b') == 'a b')  # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy