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

时间:2012-07-10 02:55:59

标签: python

  

可能重复:
  Decode HTML entities in Python string?

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

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

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

1 个答案:

答案 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?

Decoding HTML entities with Python

Decoding HTML Entities With Python