Python escape unescape html

在做网络爬虫的时候经常需要unescape得到的html,

因为得到的html经常如下:

html = '<abc>'

在python 3 中如下:

from html.parser import HTMLParser

html = '<abc>'
html_parser = HTMLParser()
txt = html_parser.unescape(html)

 得到的结果如下:

txt = '<abc>'

 如果要转回去,可以用cgi模块

import cgi

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

 

posted @ 2017-12-18 20:28  IT胖子  阅读(3329)  评论(1)    收藏  举报