使用正则表达式和python替换HTML标签 [英] HTML tag replacement using regex and python

查看:96
本文介绍了使用正则表达式和python替换HTML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python脚本,它将查看具有以下格式的HTML文件:

 < DOC> 
< HTML>
...
< / HTML>
< / DOC>
< DOC>
< HTML>
...
< / HTML>
< / DOC>

如何删除所有HTML标记(用'替换标记)并在Python中使用正则表达式关闭DOC标签?另外,如果我想保留标签的alt-text,那么正则表达式应该是什么样子? 解决方案

搜索和替换为这个正则表达式:搜索:<。*?>替换为:


I have a Python script that will look at an HTML file that has the following format:

<DOC>
<HTML>
...
</HTML>
</DOC>
<DOC>
<HTML>
...
</HTML>
</DOC>

How do I remove all HTML tags (replace the tags with '') with the exception of the opening and closing DOC tags using regex in Python? Also, if I want to retain the alt-text of an tag, what should the regex expression look like?

解决方案

search and replace with this regex: search for: <.*?> replace with: "

这篇关于使用正则表达式和python替换HTML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆