Python – Convert HTML Characters To Strings
Prerequisites: html module
Given a string with HTML characters, the task is to convert HTML characters to a string. This can be achieved with the help of html.escape() method(for Python 3.4+), we can convert the ASCII string into HTML script by replacing ASCII characters with special characters by using html.escape() method.
By this method we can decode the HTML entities into text.
Syntax:
html.unescape(String)
We can also use Beautiful Soup which handles entity conversion. In Beautiful Soup 4, entities get decoded automatically.
Example 1: Python 3.6+
Python3
import html
text = 'Γeeks for Γeeks'
print (html.unescape(text))
print (html.escape(text))
|
Output:
Γeeks for Γeeks
Γeeks for Γeeks
Example 2: Python 2.6-3.3
We can use HTMLParser.unescape() from the standard library:
- For Python 2.6-2.7 it’s in HtmlParser.
- For Python 3 it’s in html.parser
Python3
import html
try :
from HTMLParser import HTMLParser
except ImportError:
from html.parser import HTMLParser
h = html.parser
print (h.unescape( 'Γeeks for Γeeks' ))
|
Output:
Γeeks for Γeeks
Last Updated :
08 Dec, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...