首页 >> 综合精选 >

python怎么去除html标签

2022-11-07 14:57:01 来源: 用户: 

跟大家讲解下有关python怎么去除html标签,相信小伙伴们对这个话题应该也很关注吧,现在就为小伙伴们说说python怎么去除html标签,小编也收集到了有关python怎么去除html标签的相关资料,希望大家看到了会喜欢。

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标签的详细内容,更多请关注php中文网其它相关文章!

来源:php中文网

  免责声明:本文由用户上传,与本网站立场无关。财经信息仅供读者参考,并不构成投资建议。投资者据此操作,风险自担。 如有侵权请联系删除!

 
分享:
最新文章