Home  >  Article  >  Backend Development  >  How to remove html tags in python

How to remove html tags in python

藏色散人
藏色散人Original
2021-04-28 11:00:546369browse

Python method to remove html tags: 1. "pattern.sub('',html)" method; 2. "BeautifulSoup(html,'html.parser')" method; 3. "response.xpath" ('string(.)')" method.

How to remove html tags in python

The operating environment of this article: Windows 7 system, python version 3.6.4, DELL G3 computer.

Several ways to remove html tags in python

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;))
 
 
# 你好哈哈大家好
# 你好哈哈大家好
# 你好哈哈大家好

[Recommended: python video tutorial]

The above is the detailed content of How to remove html tags in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn