python实现html转ubb代码(html2ubb)

(编辑:jimmy 日期: 2024/10/4 浏览:2)

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

import re
def Html2UBB(content):
	#以下是将html标签转为ubb标签
	pattern = re.compile( '<a href=\"([sS]+"[^>]*>([sS]+"([^\"]+)\"[^>]*>',re.I)
	content = pattern.sub(r'[img]1[/img]',content)
	pattern = re.compile( '<strong>([sS]+"([sS]+">([sS]+"')
	content = content.replace('&copy;','©')
	content = content.replace('&reg;','®')
	content = content.replace('&nbsp;',' ')
	content = content.replace('&mdash;','—')
	content = content.replace('&ndash;','–')
	content = content.replace('&lsaquo;','‹')
	content = content.replace('&rsaquo;','›')
	content = content.replace('&hellip;','…')
	content = content.replace('&amp;','&')
	return content

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