How to remove html tags in python

藏色散人
Release: 2023-01-05 16:12:17
Original
6436 people have browsed it

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

[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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!