html的escape和unescape

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

用python处理html代码的转义与还原

转义 escape:

import cgi

s = cgi.escape("""& < >""")# s = '&amp; &lt; &gt;'

反转义 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转义字符抓网页数据经常遇到例如&gt;或者&nbsp;这种HTML转义符,抓到字符串里很是烦人。

比方说一个从网页中抓到的字符串

html ='&lt;abc&gt;'

用Python可以这样处理:

importHTMLParser

html_parser =HTMLParser.HTMLParser()

txt = html_parser.unescape(html)#这样就得到了txt = '<abc>'

如果还想转回去,可以这样:

importcgi

html = cgi.escape(txt)#这样又回到了 html = '&lt;abc&gt'

来回转的功能还分了两个模块实现,挺奇怪。没找到更优美的方法,欢迎补充哈~

--------------------------------------------------

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.

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航