要删除HTML标记以外的所有内容,可以使用正则表达式。以下是一个Python示例,演示如何使用正则表达式删除HTML标记以外的所有内容:
import re
def remove_html_tags(text):
clean = re.compile('<.*?>')
return re.sub(clean, '', text)
html_text = '<html><head<title>Title</title></head><body><p>This is a paragraph.</p></body></html>'
text_only = remove_html_tags(html_text)
print(text_only)
输出结果:
TitleThis is a paragraph.
这个示例中,我们定义了一个名为remove_html_tags
的函数,它接受一个包含HTML标记的字符串,并返回一个不包含HTML标记的字符串。我们使用正则表达式<.*?>
来匹配HTML标记,并使用re.sub()
函数将其替换为空字符串。
领取专属 10元无门槛券
手把手带您无忧上云