首頁  >  文章  >  後端開發  >  python怎麼去除html標籤

python怎麼去除html標籤

藏色散人
藏色散人原創
2021-04-28 11:00:546419瀏覽

python去除html標籤的方法:1、「pattern.sub('',html)」方法;2、「BeautifulSoup(html,'html.parser')」方法;3、「response.xpath ('string(.)')」方法。

python怎麼去除html標籤

本文操作環境:windows7系統、python3.6.4版,DELL G3電腦。

python去除html標籤的幾種方法

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

【推薦:python影片教學

以上是python怎麼去除html標籤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn