Categories
Python Answers

How to decode HTML entities in a Python string?

Spread the love

Sometimes, we to decode HTML entities in a Python string.

In this article, we’ll look at how to decode HTML entities in a Python string.

How to decode HTML entities in a Python string?

To decode HTML entities in a Python string, we can use the Beautiful Soup library.

To install it, we run:

pip install bs4

Then we write:

from bs4 import BeautifulSoup

html = BeautifulSoup("<p>&pound;682m</p>")
print(html)

We instantiate the BeautifulSoup class with a string with some HTML entities in it.

Then we assign the returned object to html.

Therefore, html is '<p>£682m</p>'.

Conclusion

To decode HTML entities in a Python string, we can use the Beautiful Soup library.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *