In this article, we are going to learn the Python HTML module. In Python, the HTML module is a built-in module that is only used with HTML. In the previous tutorial, we have seen all about the Python platform module to get information about the Machine.
If we want to work with HTML, then you can use the python HTML built-in module. Sometimes we want to encode and decode our HTML code, then Python provides the best module HTML which is used to encode and decode HTML code.
Headings of Contents
Python HTML module
Python HTML module is a built-in module that means you don’t need to install using pip in your system. Python HTML module is already installed when you install python in your system.
To use the Python HTML module, you have to import the HTML module using the import keyword. Basically, the HTML module in Python is used, when we want to manipulate our HTML code.
We have a simple HTML code, which is used for encoding and decoding using the Python HTML module.
<!DOCTYPE html>
<html>
<head>
<title>This is demo site </title>
</head>
<body>
<h1 style="text-align:center;">Progamming Funda is best place to learn programming </h1>
</body>
</html>
Python HTML Module Example:
Here we will use Python HTML internal module to HTML encode and HTML decode.
html.escape():
In Python HTML built-in module, the escape() method is used to encode the HTML code.
Let’s see how you can encode your HTML code using html.escape() method.
Example
import html
myhtml = '<!DOCTYPE html> <html> <head> <title>This is demo site </title></head><body><h1 style="text-align:center;">Progamming Funda is best place to learn programming </h1></body></html>'
result = html.escape(myhtml)
print(result)
Output
<!DOCTYPE html> <html> <head> <title>This is demo site </title></head><body><h1 style="text-align:center;">Progamming Funda is best place to learn programming </h1></body></html>
html.unescape():
In Python HTML built-in module, html.unescape() method is used to convert all the named and numeric characters into strings. html.unescape() method takes only one parameter that is encoded string.
Example
import html encoded = '<!DOCTYPE html> <html> <head> <title>This is demo site </title></head><body><h1 style="text-align:center;">Progamming Funda is best place to learn programming </h1></body></html>' result = html.unescape(encoded) print(result)
Output
<!DOCTYPE html> <html> <head> <title>This is demo site </title></head><body><h1 style="text-align:center;">Progamming Funda is best place to learn programming </h1></body></html>
Conclusion
In this tutorial, you have learned all about the Python HTML module to encoding and decoding HTML code. This is the best Python Module if you are want to encode and decode your HTML code.
I hope this tutorial will help you, If you like this article, please comment and share with your friends who want to learn Python programming from scratch to advanced.
For More Information:- Click Here