找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 教程频道 查看内容

python怎么去除html标签

作者:模板之家 2021-4-28 13:33 110人关注

python去除html标签的方法:1、“pattern.sub('',html)”方法;2、“BeautifulSoup(html,'html.parser')”方法;3、“response.xpath('string(.)')”方法。

python去除html标签的方法:1、“pattern.sub('',html)”方法;2、“BeautifulSoup(html,'html.parser')”方法;3、“response.xpath('string(.)')”方法。

本文操作环境:windows7系统、python3.6.4版,DELL G3电脑。

python去除html标签的几种方法

import re
from bs4 import BeautifulSoup
from lxml import etree
 
html = '<p>你好</p><br/><font>哈哈</font><b>大家好</b>'
 
# 方法一
pattern = re.compile(r'<[^>]+>',re.S)
result = pattern.sub('', html)
print(result)
 <br># 方法二
soup = BeautifulSoup(html,'html.parser')
print(soup.get_text())
 
# 方法三
response = etree.HTML(text=html)
# print(dir(response))
print(response.xpath('string(.)'))
 
 
# 你好哈哈大家好
# 你好哈哈大家好
# 你好哈哈大家好

【推荐:python视频教程】

以上就是python怎么去除html标签的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章!


路过

雷人

握手

鲜花

鸡蛋
来自: 网络收集

全部回复(0)