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

用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可以这样处理:

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

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

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

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

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

html的escape和unescape

原文地址:https://www.cnblogs.com/kungfupanda/p/4313903.html