views:

91

answers:

2

Hi folks,

I used BeautifulSoup to handle XML files that I have collected through a REST API.

The responses contain HTML code, but BeautifulSoup escapes all the HTML tags so it can be displayed nicely.

Unfortunately I need the HTML code.


How would I go on about transforming the escaped HTML into proper markup?


Help would be very much appreciated!

+5  A: 

I think you want xml.sax.saxutils.unescape from the Python standard library.

E.g.:

>>> from xml.sax import saxutils as su
>>> s = '<foo>bar</foo>'
>>> su.unescape(s)
'<foo>bar</foo>'
Alex Martelli
@Alex thank you!! this was nice and simple! :D
RadiantHex
+1  A: 

You could try the urllib module?

It has a method unquote() that might suit your needs.

Edit: on second thought, (and more reading of your question) you might just want to just use string.replace()

Like so:

string.replace('&lt;','<')
string.replace('&gt;','>')
George Edison
Why would you bother with coding the different replace steps (for lt, gt, amp) when the saxutils.unescape wraps them all up for you?-) Plus, remember: the replace call doesn't alter the string, it builds a new string. Your code snippet, as given, is a slow no-op!-)
Alex Martelli