python实现html转ubb代码(html2ubb)

yipeiwu_com6年前Python基础

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

import re
def 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

相关文章

Win10下python 2.7与python 3.7双环境安装教程图解

Win10下python 2.7与python 3.7双环境安装教程图解

Win10下python 2.7与python 3.7双环境安装教程,具体内容如下所示: 1、python软件下载网址: https://www.python.org/downloads...

python http接口自动化脚本详解

python http接口自动化脚本详解

今天给大家分享一个简单的python脚本,使用python进行http的接口测试,脚本很简单,逻辑是:读取excel写好的测试用例,然后根据excel中的用例内容进行调用,判断预期结果中...

pytorch 实现查看网络中的参数

可以通过model.state_dict()或者model.named_parameters()函数查看现在的全部可训练参数(包括通过继承得到的父类中的参数) 可示例代码如下:...

Python中多个数组行合并及列合并的方法总结

采用numpy快速将两个矩阵或数组合并成一个数组: import numpy as np 数组 a = [[1,2,3],[4,5,6]] b = [[1,1,1],[2,2,...

用python处理图片实现图像中的像素访问

用python处理图片实现图像中的像素访问

前面的一些例子中,我们都是利用Image.open()来打开一幅图像,然后直接对这个PIL对象进行操作。如果只是简单的操作还可以,但是如果操作稍微复杂一些,就比较吃力了。因此,通常我们加...