Python method to remove html tags: 1. "pattern.sub('',html)" method; 2. "BeautifulSoup(html,'html.parser')" method; 3. "response.xpath" ('string(.)')" method.
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 = '<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(.)')) # 你好哈哈大家好 # 你好哈哈大家好 # 你好哈哈大家好
[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!