def decode_htmlentities(string):
    """
    Decode HTML entities–hex, decimal, or named–in a string
    @see http://snippets.dzone.com/posts/show/4569
    >>> u = u'E tu vivrai nel terrore - L'aldilà (1981)'
    >>> print decode_htmlentities(u).encode('UTF-8')
    E tu vivrai nel terrore - L'aldilà (1981)
    >>> print decode_htmlentities("l'eau")
    l'eau
    >>> print decode_htmlentities("foo < bar")                
    foo < bar
    """

def substitute_entity(match):
    ent = match.group(3)
if match.group(1) == '#':
    if match.group(2) == '':
        return chr(int(ent))
elif match.group(2) == 'x':
    return chr(int('0x' + ent, 16))
else:
    cp = n2cp.get(ent)
if cp:
    return chr(cp)
else:
    return match.group()
entity_re = re.compile('&(#?)(x?)(\\w+);')
return entity_re.subn(substitute_entity, string)[0]
How to unescape apostrophes and such in Python?