7

可能重复:
解码 Python 字符串中的 HTML 实体?

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

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

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

4

1 回答 1

18

你想用这个:

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 的喜爱

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

这些现有问题的重复:

在 Python 字符串中解码 HTML 实体?

使用 Python 解码 HTML 实体

使用 Python 解码 HTML 实体

于 2012-07-10T03:04:14.287 回答