转义 escape:
import cgi
s = cgi.escape("""& < >""") # s = '& < >'
反转义 unescape:
#使用标准库
from htmllib import HTMLParser
h = HTMLparser.HTMLParser()
s = h.unescape('& < >') # s = u'& < >'
#使用BeautifulSoup
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, convertEntities=BeautifulSoup.HTML_ENTITIES)
引用于:
http://fredericiana.com/2010/10/08/decoding-html-entities-to-text-in-python/
https://wiki.python.org/moin/EscapingHtml
http://blog.163.com/gaocheng2008@yeah/blog/static/93043209201431632148859/