PHP前端开发

python怎么去除html标签

百变鹏仔 1个月前 (01-22) #Python
文章标签 标签
python去除html标签的方法:1、“pattern.sub('',html)”方法;2、“BeautifulSoup(html,'html.parser')”方法;3、“response.xpath('string(.)')”方法。

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

python去除html标签的几种方法

import refrom bs4 import BeautifulSoupfrom lxml import etree html = '<p>你好</p><br><font>哈哈</font><b>大家好</b>' # 方法一pattern = re.compile(r']+&gt;',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免费学习笔记(深入)”;