python怎么去除html标签

python怎么去除html标签,第1张

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标签的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/680803.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-20
下一篇 2022-04-20

发表评论

登录后才能评论

评论列表(0条)

保存