Home >Backend Development >Python Tutorial >How to Decode HTML Entities in Python Strings?

How to Decode HTML Entities in Python Strings?

Barbara Streisand
Barbara StreisandOriginal
2024-12-14 05:33:09227browse

How to Decode HTML Entities in Python Strings?

How to Decode HTML Entities in Python String?

When parsing HTML with Beautiful Soup 3, HTML entities often appear and need to be decoded. This can be done using the html.unescape() or HTMLParser.unescape() function.

Python 3.4

Use html.unescape():

import html
html.unescape('£682m')

Python 2.6-3.3

From HTMLParser in Python 2.6-2.7 or html.parser in Python 3, unescape():

from html.parser import HTMLParser
parser = HTMLParser()
print(h.unescape('£682m'))

Alternatively, with the six compatibility library:

from six.moves.html_parser import HTMLParser
parser = HTMLParser()
print(h.unescape('£682m'))

The above is the detailed content of How to Decode HTML Entities in Python Strings?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn