使用Python从HTML中删除标记可以通过以下几种方法实现:
from bs4 import BeautifulSoup
def remove_tags(html):
soup = BeautifulSoup(html, 'html.parser')
text = soup.get_text()
return text
html = '<p>This is a <b>sample</b> HTML.</p>'
text = remove_tags(html)
print(text)
输出结果为:This is a sample HTML.
推荐的腾讯云相关产品:无
import re
def remove_tags(html):
cleanr = re.compile('<.*?>')
text = re.sub(cleanr, '', html)
return text
html = '<p>This is a <b>sample</b> HTML.</p>'
text = remove_tags(html)
print(text)
输出结果为:This is a sample HTML.
推荐的腾讯云相关产品:无
from html.parser import HTMLParser
class MyHTMLParser(HTMLParser):
def __init__(self):
super().__init__()
self.text = ''
def handle_data(self, data):
self.text += data
def remove_tags(html):
parser = MyHTMLParser()
parser.feed(html)
return parser.text
html = '<p>This is a <b>sample</b> HTML.</p>'
text = remove_tags(html)
print(text)
输出结果为:This is a sample HTML.
推荐的腾讯云相关产品:无
以上是使用Python从HTML中删除标记的几种方法,可以根据具体需求选择适合的方法进行处理。
腾讯云GAME-TECH游戏开发者技术沙龙
腾讯云GAME-TECH游戏开发者技术沙龙
云+社区技术沙龙[第14期]
云+社区技术沙龙[第5期]
云+社区技术沙龙[第28期]
腾讯云GAME-TECH游戏开发者技术沙龙
云+社区技术沙龙[第27期]
腾讯云GAME-TECH游戏开发者技术沙龙
云+社区技术沙龙[第7期]
北极星训练营
领取专属 10元无门槛券
手把手带您无忧上云