I would like to know what would be the most efficient way to clean all of the HTML tags from this string using python.
<p>Lorem ipsum dolor sit amet, <strong>consectetur adipiscing
elit</strong></p>,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</br>
<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</br>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
<a href="">Excepteur sint occaecat</a>
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
I tried using replace('[insert tag]', '') but I have to create multiple replace() lines to remove all of the tags.
CodePudding user response:
You can try
import re
def remove_html_tags(text):
clean = re.compile('<.*?>')
return re.sub(clean, '', text)
There are several ways to remove HTML tags from a string in Python.
Hope it helped
CodePudding user response:
REGEX
The easiest way is to use regex to remove the tags. To do this, you need to first import re
. Then you want to remove everything inside <>. To do this, you use remover = re.compile('<.*?>')
The full code should look something like this:
import re
string = '''<p>Lorem ipsum dolor sit amet, <strong>consectetur adipiscing
elit</strong></p>,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</br>
<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</br>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
<a href="">Excepteur sint occaecat</a>
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>'''
remover = re.compile('<.*?>')
print(re.sub(remover, '', string))
LXML
Another way is to use the lxml
library, which can be installed with pip install lxml
The lxml module has a built in function to remove all the tags from html.
The code would look something like this:
from lxml import html
string = '''<p>Lorem ipsum dolor sit amet, <strong>consectetur adipiscing
elit</strong></p>,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</br>
<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</br>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
<a href="">Excepteur sint occaecat</a>
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>'''
print(html.fromstring(string).text_content())
The code html.fromstring(string).text_content()
is what converts the html into text, and thus removes all the tags from it.
CodePudding user response:
One way to remove HTML tags from a string is using Beautiful Soup library. Calling the text() function extracts all the text and strips all tags.
BeautifulSoup will parse and remove only HTML tags and not just remove any sequence of text starting with a '<' and ending with a '>'. It will also auto-translate the HTML entities; e.g. <
=> '<', >
=> '>', etc.
from bs4 import BeautifulSoup
html = """<p>Lorem ipsum dolor sit amet, <strong>consectetur adipiscing
elit</strong></p>,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</br>
<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</br>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
<a href="">Excepteur sint occaecat</a>
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>"""
soup = BeautifulSoup(html, "html.parser")
print(soup.text)
Output:
Lorem ipsum dolor sit amet, consectetur adipiscing
elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.