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空指针异常解决方法
Throwable是所有错误或异常的超类,只有当对象是这个类的实例时才能通过Java虚拟机或者Java throw语句抛出. 当Java运行环境发出异常时,会寻找处理该异常的catch块,找到对应的c ...
- ElasticSearch1.7.1拼音插件elasticsearch-analysis-pinyin-1.3.3使用介绍
ElasticSearch拼音插件elasticsearch-analysis-pinyin使用介绍 https://my.oschina.net/xiaohui249/blog/214505 摘要: ...
- kibana和ElasticSearch的信息查询检索
使用kibana来进行ElasticSearch的信息查询检索 大家经常会听到使用ELK搭建日志管理平台.完成日志聚合检索的功能,那么这个平台到底是个什么概念,怎么搭建,怎么使用呢? ELK包括Ela ...
- Augmenting Path Algorithm : 一般图最大匹配
算法原理详见 http://www.csie.ntnu.edu.tw/~u91029/Matching.html orz 带花树很神奇,挖坑最大权匹配 #include <iostream> ...
- STL map、set中key为结构体的用法
下面是map定义的结构: // TEMPLATE CLASS map template<class _Kty, class _Ty, class _Pr = less<_Kty>, ...
- Reasons to use innodb_file_per_table
When working with InnoDB, you have two ways for managing the tablespace storage: Throw everything in ...
- Virtualization solutions on Linux systems - KVM and VirtualBox
Introduction Virtualization packages are means for users to run various operating systems without &q ...
- hadoop之shuffle详解
Shuffle描述着数据从map task输出到reduce task输入的这段过程. 如map 端的细节图,Shuffle在reduce端的过程也能用图上标明的三点来概括.当前reduce copy ...
- 利用saltstack初始化OpenStack服务器环境
目录架构图如上图所示 sls脚本详情如下: Sync_Host: file.managed: - name: /etc/hosts - source: salt://state/files/hosts ...
- [bzoj1588][HNOI2002]营业额统计——splay
题目大意 你被要求编写一个数据结构,支援以下操作,操作在线. 插入一个元素 查询一个元素与之前插入元素的最小差值. 题解 一道模板题.我是写了一个pre和succ函数水过的.1A,比较高兴. 代码 # ...