使用 python,从字符串中删除 HTML 标签/格式 [英] using python, Remove HTML tags/formatting from a string

查看:34
本文介绍了使用 python,从字符串中删除 HTML 标签/格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,其中包含 html 标记,如链接、粗体文本等.

I have a string that contains html markup like links, bold text, etc.

我想去掉所有标签,所以我只有原始文本.

I want to strip all the tags so I just have the raw text.

这样做的最佳方法是什么?正则表达式?

What's the best way to do this? regex?

推荐答案

如果您打算使用正则表达式:

If you are going to use regex:

import re
def striphtml(data):
    p = re.compile(r'<.*?>')
    return p.sub('', data)

>>> striphtml('<a href="foo.com" class="bar">I Want This <b>text!</b></a>')
'I Want This text!'

这篇关于使用 python,从字符串中删除 HTML 标签/格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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