1. 参考
HTML 字符实体
比方说一个从网页中抓到的字符串
html = '<abc>'
用Python可以这样处理:
import HTMLParser html_parser = HTMLParser.HTMLParser() txt = html_parser.unescape(html) #这样就得到了txt = '<abc>'
如果还想转回去,可以这样:
import cgi html = cgi.escape(txt) # 这样又回到了 html = '<abc>'
2. 常用
| 显示结果 | 描述 | 实体名称 | 实体编号 |
|---|---|---|---|
| 空格 | |   | |
| < | 小于号 | < | < |
| > | 大于号 | > | > |
| & | 和号 | & | & |
| " | 引号 | " | " |
| ' | 撇号 | ' (IE不支持) | ' |
3. 代码
1 In [354]: soup = BeautifulSoup("“Dammit!” he said.")
2
3 In [355]: unicode(soup)
4 Out[355]: u'<html><body><p>\u201cDammit!\u201d he said.</p></body></html>'
5
6 In [358]: str(soup)
7 Out[358]: '<html><body><p>\xe2\x80\x9cDammit!\xe2\x80\x9d he said.</p></body></html>'
8
9 In [359]: print str(soup).decode('utf-8')
10 <html><body><p>“Dammit!” he said.</p></body></html>
11
12 In [360]: print unicode(soup)
13 <html><body><p>“Dammit!” he said.</p></body></html>
来源:https://www.cnblogs.com/my8100/p/7028601.html