看啥推荐读物
专栏名称: 大数据学习笔记
希望看过笔记的人,可以从中学到一点知识!
今天看啥  ›  专栏  ›  大数据学习笔记

在PYTHON中使用UNESCAPE HTML实体

大数据学习笔记  · 知乎专栏  ·  · 2019-03-15 09:35
这段代码可能对某些人有用,def parsefile(path): try: file = open(path, "r") fileread = file.read() fileread = unescape(fileread.decode('utf-8')).encode('utf-8') file.close() except: print "Reading File Bug" sys.exit(1) return ET.fromstring(fileread)UNESCAPE HTML实体程序已于弗雷德里克Lundh开发网站上找到。代码做得太多了,因为它正在转换&,& gt;而且<。我希望将这些保存在URL中以及我已转义代码段的位置。所以我稍微修改了它以满足我自己的需要。def unescape(text): """Removes HTML or XML character references and entities from a text string. keep &,& gt; &ltin the source code. from Fredrik Lundh http://effbot.org/zone/re-sub.htm#unescape-html """ def fixup(m): text = m.group(0) if text[:2] == "&#": # character reference try: if text[:3] == "&#x": ………………………………

原文地址:访问原文地址
快照地址: 访问文章快照