HTML实体codeS为文本 [英] HTML Entity Codes to Text

查看:171
本文介绍了HTML实体codeS为文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道在Python中一个简单的方法将字符串转换与HTML实体codeS(如&放大器; LT; &放大器;放大器; ),以一个正常的字符串(例如<&安培;)

Does anyone know an easy way in Python to convert a string with HTML entity codes (e.g. &lt; &amp;) to a normal string (e.g. < &)?

cgi.escape()将难逃字符串(很差),但没有 UNESCAPE()

cgi.escape() will escape strings (poorly), but there is no unescape().

推荐答案

的HTMLParser 在标准功能图书馆。这是不幸的是,没有证件:

HTMLParser has the functionality in the standard library. It is, unfortunately, undocumented:

>>> import HTMLParser
>>> h= HTMLParser.HTMLParser()
>>> h.unescape('alpha &lt; &beta;')
u'alpha < \u03b2'

htmlentitydefs的是记录,但需要你做了很多的工作自己。

htmlentitydefs is documented, but requires you to do a lot of the work yourself.

如果您只需要XML predefined实体(LT,GT,放大器和quot,者),你可以使用minidom命名解析他们。如果你只需要predefined实体和没有数字字符引用,你甚至可以只使用一个普通的旧字符串替换速度。

If you only need the XML predefined entities (lt, gt, amp, quot, apos), you could use minidom to parse them. If you only need the predefined entities and no numeric character references, you could even just use a plain old string replace for speed.

这篇关于HTML实体codeS为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆