python strip_tags 支持保留指定标签
#coding:utf-8 import re def strip_tags(string, allowed_tags=''):
if allowed_tags != '':
# Get a list of all allowed tag names.
allowed_tags = allowed_tags.split(',')
allowed_tags_pattern = ['</?'+allowed_tag+'[^>]*>' for allowed_tag in allowed_tags]
all_tags = re.findall(r'<[^>]+>', string, re.I)
not_allowed_tags = []
tmp = 0
for tag in all_tags:
for pattern in allowed_tags_pattern:
rs = re.match(pattern,tag)
if rs:
tmp += 1
else:
tmp += 0
if not tmp:
not_allowed_tags.append(tag)
tmp = 0
for not_allowed_tag in not_allowed_tags:
string = re.sub(re.escape(not_allowed_tag), '',string)
print not_allowed_tags
else:
# If no allowed tags, remove all.
string = re.sub(r'<[^>]*?>', '', string) return string
python strip_tags 支持保留指定标签的更多相关文章
- C#正则过滤HTML标签并保留指定标签的方法
本文实例讲述了C#正则过滤html标签并保留指定标签的方法.分享给大家供大家参考,具体如下: 这边主要看到一个过滤的功能: public static string FilterHtmlTag(str ...
- 正则去除html标签属性保留指定标签
/// <summary> /// 去除标签里面的属性保留IMG标签属性 /// </summary> /// <param name="strText&quo ...
- python保留指定文件、删除目录其他文件的功能(1)
由于给客户的发布版本上客户改动了些代码和图片,我们这边给他们更新publish都是增量更新(开发提供更新指定的文件,我们提取出来给客户进行覆盖更新),但有时需要更新的文件较多导致不得不一个一个的进行查 ...
- Python保留指定位数的小数
Python保留指定位数的小数 1 '%.2f' %f 方法(推荐) f = 1.23456 print('%.4f' % f) print('%.3f' % f) print('%.2f' % f) ...
- python保留指定文件、删除目录其他文件的功能(2)
在(1)中脚本实现了保留指定文件的功能,但不能删除空目录,在此补上删除空目录的方法 def DeleteEmptyDir(path): for i in range(1,100): for paren ...
- android TextView加载html 过滤所有标签,保留换行标签
情景: TextView加载后端接口获取到的html富文本 遇到的问题: 客户端通过接口取到的数据如下: <p style="margin-top: 0px; margin-botto ...
- Python批量图片识别并翻译——我用python给女朋友翻译化妆品标签
Python批量图片识别并翻译--我用python给女朋友翻译化妆品标签 最近小编遇到一个生存问题,女朋友让我给她翻译英文化妆品标签.美其名曰:"程序猿每天英语开发,英文一定很好吧,来帮我翻 ...
- C# decimal保留指定的小数位数,不四舍五入
decimal保留指定位数小数的时候,.NET自带的方法都是四舍五入的. 项目中遇到分摊金额的情况,最后一条的金额=总金额-已经分摊金额的和. 这样可能导致最后一条分摊的时候是负数,所以自己写了一个保 ...
- 修改XML指定标签的内容
修改Xml指定标签内容(我这是去掉指定标签内容的空格) 其实就是个很简单的方法,需要的盆友直接拿走. test.xml <?xml version="1.0" encodin ...
随机推荐
- 【转】Java线程系列:Callable和Future
一.前言 在研究JDK1.8的CompletableFuture时,顺道将Futrue一起扫了盲~这篇博文纯转载 二.正文 本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一 ...
- typescript 贪吃蛇[学习过程中,模仿的一个例子]
代码实现ts: 1 'use strict' module Main { const FloorType = { space: "space", snack: "body ...
- C#中的SubString()的用法
先看语法: String.SubString(int index,int length) index:开始位置,从0开始 length:你要取的子字符串的长度 例子: using ...
- eclipse启运时显示:Workspace in use or cannot be created, choose a different one
The time when I runned Eclipse in my computer, it has this information displayed: WorkSpace *** in u ...
- 【题解】HAOI2011Problem b
第一次接触莫比乌斯反演,总之脑子都快要炸掉了……好难啊!本蒟蒻表示根本无法理解呜呜呜呜呜……不过在机房DL的帮助下总算是磕磕绊绊的A掉了这一题: 这道题目要我们的求的是:(1)ΣiΣj [gcd(i, ...
- C&C++——extern
1.C 调用C++的函数或变量 C 调用C++的函数或变量,在C++的头文件声明为extern "C" ,C调用的时候只使用extern 声明. 可见,extern "C ...
- Seajs的用法
以前经常听到Seajs,但是没深入了解过,不清楚到底是用做哪个方面,后来调组到M站做开发,发现项目用到了Seajs,便去了解下 SeaJS是一个遵循CMD规范的JavaScript模块加载框架,可以实 ...
- bulk_insert_buffer_size and InnoDB
Q: I read the following on this page http://dev.mysql.com/doc/mysql/en/server-system-variables.html ...
- struts2学习问题(一)
一.struts2 Unknown tag (s:property). 解释:不识别标签 解决:这是sturts2的标签,导入相应的包<%@taglib prefix="s" ...
- 着色方案(bzoj 1079)
Description 有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木 ...