Chinaunix
标题:
python如何将html数字编码后的文本还原?
[打印本页]
作者:
iocg
时间:
2014-11-10 16:41
标题:
python如何将html数字编码后的文本还原?
本帖最后由 iocg 于 2014-11-10 16:52 编辑
python有什么方法可以将html数字编码后的文本,还原?
例如:
2.png
(306.26 KB, 下载次数: 12)
下载附件
2014-11-10 16:48 上传
还原成
我爱 <chinaunix> 论坛
I love "chinaunix" Forums
私は&chinaunix&フォーラムが大好き
Je l'àime ¿chinaunix¿ Förums
复制代码
1.rar
(236 Bytes, 下载次数: 3)
2014-11-10 16:51 上传
点击文件名下载附件
html编码后
作者:
ssfjhh
时间:
2014-11-10 18:06
本帖最后由 ssfjhh 于 2014-11-11 10:40 编辑
list(int, re.findall(string, '\d'))
复制代码
作者:
reyleon
时间:
2014-11-10 19:16
回复
2#
ssfjhh
这也行?
作者:
iocg
时间:
2014-11-10 21:28
回复
3#
reyleon
在网上找到HTMLParser库好像可以实现。
HTMLParser采用的是一种事件驱动的模式,当TMLParser找到一个特定的标记时,它会去调用一个用户定义的函数,以此来通知程序处理。它主要的用户回调函数的命名都是以handler_开头的,都是HTMLParser的成员函数。当我们使用时,就从HTMLParser派生出新的类,然 后重新定义这几个以handler_开头的函数即可。
handle_charref 处理特殊字符串,就是以&#开头的,一般是内码表示的字符
handle_entityref 处理一些特殊字符,以&开头的,比如
handle_data 处理数据,就是<xx>data</xx>中间的那些数据
请问下知道具体代码怎么实现吗?
作者:
bikong0411
时间:
2014-11-11 09:56
import re
try:
from htmlentitydefs import entitydefs
except ImportError: # Python 3
from html.entities import entitydefs
def htmlspecialchars_decode_func(m, defs=entitydefs):
try:
return defs[m.group(1)]
except KeyError:
return m.group(0) # use as is
def htmlspecialchars_decode(string):
pattern = re.compile("&(\w+?);")
return pattern.sub(htmlspecialchars_decode_func, string)
作者:
iocg
时间:
2014-11-11 22:46
回复
5#
bikong0411
请问这m该传入怎样的值呢?
作者:
bikong0411
时间:
2014-11-13 09:31
回复
6#
iocg
htmlspecialchars_decode(string) ,你传入string就可以了
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2