如何使用Python替换HTML转义字符?

How can I use Python to replace HTML escape characters?

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
Decode HTML entities in Python string?

我有一个充满HTML转义字符的字符串,例如"

是否有任何Python库为我提供了可靠的方式来将所有这些转义字符替换为其各自的实际字符?

例如,我希望将所有"替换为" s"。


您要使用此:

1
2
3
4
5
6
try:
    from html.parser import HTMLParser  # Python 3
except ModuleNotFoundError:
    from HTMLParser import HTMLParser  # Python 2
parser = HTMLParser()
html_decoded_string = parser.unescape(html_encoded_string)

我也看到了许多对BeautifulSoup的热爱

1
2
from BeautifulSoup import BeautifulSoup
html_decoded_string = BeautifulSoup(html_encoded_string, convertEntities=BeautifulSoup.HTML_ENTITIES)

还重复了这些现有问题:

用Python字符串解码HTML实体?

使用Python解码HTML实体

使用Python解码HTML实体