首页 > 编程 > Python > 正文

python实现html转ubb代码(html2ubb)

2020-02-23 05:29:39
字体:
来源:转载
供稿:网友

这两天在用python写一个采集器,有个功能模块是html代码转换为ubb,网上貌似没有现成程序,就自己写了个函数,顺便锻炼下自己的正则。

import redef Html2UBB(content):	#以下是将html标签转为ubb标签	pattern = re.compile( '<a href=/"([sS]+?)/"[^>]*>([sS]+?)</a>',re.I)	content = pattern.sub(r'[url=1]2[/url]',content)	pattern = re.compile( '<img[^>]+src=/"([^/"]+)/"[^>]*>',re.I)	content = pattern.sub(r'[img]1[/img]',content)	pattern = re.compile( '<strong>([sS]+?)</strong>',re.I)	content = pattern.sub(r'[b]1[/b]',content)	pattern = re.compile( '<font color=/"([sS]+?)/">([sS]+?)</font>',re.I)	content = pattern.sub(r'[1]2[/1]',content)	pattern = re.compile( '<[^>]*?>',re.I)	content = pattern.sub('',content)	#以下是将html转义字符转为普通字符	content = content.replace('<','<')	content = content.replace('>','>')	content = content.replace('”','”')	content = content.replace('“','“')	content = content.replace('"','"')	content = content.replace('©','©')	content = content.replace('®','®')	content = content.replace(' ',' ')	content = content.replace('—','—')	content = content.replace('–','–')	content = content.replace('‹','‹')	content = content.replace('›','›')	content = content.replace('…','…')	content = content.replace('&','&')	return content

使用时直接调用Html2UBB函数,返回值就是ubb码了html转ubb

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表