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

How to Decode HTML Entities in Python Strings?

Barbara Streisand
Release: 2024-12-14 05:33:09
Original
224 people have browsed it

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')
Copy after login

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'))
Copy after login

Alternatively, with the six compatibility library:

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template