Python HTMLParser处理HTML转义字符
抓网页数据经常遇到例如>或者 这种HTML转义符,抓到字符串里很是烦人。比方说一个从网页中抓到的字符串:
1html = '<abc>'
用Python可以这样处理:
1import HTMLParser
2html_parser = HTMLParser.HTMLParser()
3txt = html_parser.unescape(html) #这样就得到了txt = '<abc>'
如果还想转回去,可以这样:
1import cgi
2html = cgi.escape(txt) # 这样又回到了 html = '<abc>'
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/python-htmlparser/3979.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.