python如何将html数字编码后的文本还原?
本帖最后由 iocg 于 2014-11-10 16:52 编辑python有什么方法可以将html数字编码后的文本,还原?
例如:
还原成我爱 <chinaunix> 论坛
I love "chinaunix" Forums
私は&chinaunix&フォーラムが大好き
Je l'àime ¿chinaunix¿ Förums 本帖最后由 ssfjhh 于 2014-11-11 10:40 编辑
list(int, re.findall(string, '\d')) 回复 2# ssfjhh
这也行? 回复 3# reyleon
在网上找到HTMLParser库好像可以实现。
HTMLParser采用的是一种事件驱动的模式,当TMLParser找到一个特定的标记时,它会去调用一个用户定义的函数,以此来通知程序处理。它主要的用户回调函数的命名都是以handler_开头的,都是HTMLParser的成员函数。当我们使用时,就从HTMLParser派生出新的类,然 后重新定义这几个以handler_开头的函数即可。
handle_charref 处理特殊字符串,就是以&#开头的,一般是内码表示的字符
handle_entityref 处理一些特殊字符,以&开头的,比如
handle_data 处理数据,就是<xx>data</xx>中间的那些数据
请问下知道具体代码怎么实现吗?
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
except KeyError:
return m.group(0)# use as is
def htmlspecialchars_decode(string):
pattern = re.compile("&(\w+?);")
return pattern.sub(htmlspecialchars_decode_func, string) 回复 5# bikong0411
请问这m该传入怎样的值呢? 回复 6# iocg
htmlspecialchars_decode(string) ,你传入string就可以了
页:
[1]