用python處理html代碼的轉義與還原
轉義 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
----------------------------------------------------------------------------------------------------------
Python處理HTML轉義字符
抓網頁數據經常遇到例如>或者 這種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>'
來回轉的功能還分了兩個模塊實現,挺奇怪。沒找到更優美的方法,歡迎補充哈~
--------------------------------------------------
html的escape和unescape
http://stackoverflow.com/questions/275174/how-do-i-perform-html-decoding-encoding-using-python-django
For html encoding, there's cgi.escape from the standard library:
>> help(cgi.escape)
cgi.escape = escape(s, quote=None)
Replace special characters "&", "<" and ">" to HTML-safe sequences.
If the optional flag quote is true, the quotation mark character (")
is also translated.
For html decoding, I use the following:
from htmlentitydefs import name2codepoint
# for some reason, python 2.5.2 doesn't have this one (apostrophe)
name2codepoint['#39'] = 39
def unescape(s):
"unescape HTML code refs; c.f. http://wiki.python.org/moin/EscapingHtml"
return re.sub('&(%s);' % '|'.join(name2codepoint),
lambda m: unichr(name2codepoint[m.group(1)]), s)
For anything more complicated, I use BeautifulSoup.
For html encoding, there's cgi.escape from the standard library:
>> help(cgi.escape)
cgi.escape = escape(s, quote=None)
Replace special characters "&", "<" and ">" to HTML-safe sequences.
If the optional flag quote is true, the quotation mark character (")
is also translated.
For html decoding, I use the following:
from htmlentitydefs import name2codepoint
# for some reason, python 2.5.2 doesn't have this one (apostrophe)
name2codepoint['#39'] = 39
def unescape(s):
"unescape HTML code refs; c.f. http://wiki.python.org/moin/EscapingHtml"
return re.sub('&(%s);' % '|'.join(name2codepoint),
lambda m: unichr(name2codepoint[m.group(1)]), s)
For anything more complicated, I use BeautifulSoup.