【发布时间】:2012-07-09 11:56:27
【问题描述】:
我有一个充满 HTML 转义字符的字符串,例如 "、” 和 —。
是否有任何 Python 库提供可靠的方法让我用它们各自的实际字符替换所有这些转义字符?
例如,我希望将所有"s 替换为“s”。
【问题讨论】:
标签: python
我有一个充满 HTML 转义字符的字符串,例如 "、” 和 —。
是否有任何 Python 库提供可靠的方法让我用它们各自的实际字符替换所有这些转义字符?
例如,我希望将所有"s 替换为“s”。
【问题讨论】:
标签: python
你想用这个:
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)
这些现有问题也重复:
Decode HTML entities in Python string?
【讨论】:
beautifulsoup4==4.6.0 和py3,这应该是pip install beautifulsoup4,然后是from bs4 import BeautifulSoup; html_decoded_string = BeautifulSoup(x, "lxml"); print(html_decoded_string.string)
from html.parser import HTMLParser。