python通过正则分别删除<img><a><storong>标签并保留内容
时间: 2024-06-02 15:06:53 浏览: 16
可以使用Python的re模块来实现通过正则表达式删除html标签。
以下是一段示例代码,可以去掉<img><a><strong>标签并保留内容:
```python
import re
def remove_html_tags(text):
clean = re.compile('<.*?>')
return re.sub(clean, '', text)
text = "<p><strong>Hello</strong> <a href='http://example.com'>world</a> <img src='image.png'></p>"
result = remove_html_tags(text)
print(result)
```
输出:
```
Hello world
```
其中,`<.*?>`是一个正则表达式,表示匹配所有的html标签。`re.sub()`方法可以用来替换字符串中的匹配项,这里用它来将匹配到的html标签替换成空字符串。
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)