- #!/usr/bin/env python3
- import re
- def remove_html_event(html):
- '''移除html代码中的事件属性'''
- html = html.replace('\\n', '')
- #匹配html事件属性及其后面的所有属性
- patt1 = r'''<[\\w]+\\s*?.*?\\s(on[\\w]+=(["']{1}).*?\\2)>'''
- #匹配事件属性
- patt2 = r'''on[\\w]+=(["']).*?\\1'''
- p1 = re.compile(patt1, re.I)
- p2 = re.compile(patt2, re.I)
- p1_result = p1.findall(html)
- for p1_tuple in p1_result:
- p2_match = p2.match(p1_tuple[0])
- try:
- html = html.replace(p2_match.group(0), '')
- except IndexError:
- pass
- return html;
- #该片段来自于http://www.codesnippet.cn/detail/091020136326.html
来源: http://www.codesnippet.cn/detail/091020136326.html