HTML Entity Codes to Text

beautifulsoup, html, python

Solution

HTMLParser has the functionality in the standard library. It is, unfortunately, undocumented:

(Python2 Docs)

>>> import HTMLParser
>>> h= HTMLParser.HTMLParser()
>>> h.unescape('alpha < β')
u'alpha < \u03b2'

(Python 3 Docs)

>>> import html.parser
>>> h = html.parser.HTMLParser()
>>> h.unescape('alpha &lt; &beta;')
'alpha < \u03b2'

htmlentitydefs is documented, but requires you to do a lot of the work yourself.

If you only need the XML predefined entities (lt, gt, amp, quot, apos), you could use minidom to parse them. If you only need the predefined entities and no numeric character references, you could even just use a plain old string replace for speed.

Problem

Does anyone know an easy way in Python to convert a string with HTML entity codes (e.g. `&lt;` `&amp;`) to a normal string (e.g. < &)? `cgi.escape()` will escape strings (poorly), but there is no `unescape()`.

Original source

Related problems