我有一个充满HTML转义字符的字符串,例如"
,”
和—
。
是否有任何Python库为我提供了可靠的方法来将所有这些转义字符替换为各自的实际字符?
例如,我希望将所有"
替换为“s。
答案 0 :(得分:16)
你想用这个:
from HTMLParser import HTMLParser
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?