Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Convert XML/HTML Entities into Unicode String in Python [duplicate]

I'm doing some web scraping and sites frequently use HTML entities to represent non ascii characters. Does Python have a utility that takes a string with HTML entities and returns a unicode type?

For example:

I get back:

ǎ

which represents an "ǎ" with a tone mark. In binary, this is represented as the 16 bit 01ce. I want to convert the html entity into the value u'\u01ce'

Answer*

Draft saved
Draft discarded
Cancel
4
  • it also works for hex entities. The implementation is very similar to unescape() function from @dF.'s answer. Commented Oct 2, 2012 at 21:26
  • 8
    This method isn't documented in Python's HTMLParser documentation, and there's a comment in the source stating it's intended for internal use. However, it works like treat in Python 2.6 through 2.7, and is probably the best solution out there. Prior to version 2.6, it would only decode named entities like & or >. Commented Oct 17, 2012 at 0:34
  • 7
    It is exposed as html.unescape() function in Python 3.4+ Commented Oct 7, 2014 at 19:23
  • This raise UnicodeDecodeError with utf-8 strings. You must either decode('utf-8') it first or use xml.sax.saxutils.unescape. Commented Oct 7, 2016 at 12:55

default

AltStyle によって変換されたページ (->オリジナル) /