Strip HTML from strings in Python
You can use BeautifulSoup get_text()
feature.
from bs4 import BeautifulSoup
html_str = '''
<td><a href="http://www.fakewebsite.example">Please can you strip me?</a>
<br/><a href="http://www.fakewebsite.example">I am waiting....</a>
</td>
'''
soup = BeautifulSoup(html_str)
print(soup.get_text())
#or via attribute of Soup Object: print(soup.text)
It is advisable to explicitly specify the parser, for example as BeautifulSoup(html_str, features="html.parser")
, for the output to be reproducible.
If you need to strip HTML tags to do text processing, a simple regular expression will do. Do not use this if you are looking to sanitize user-generated HTML to prevent XSS attacks. It is not a secure way to remove all <script>
tags or tracking <img>
s. The following regular expression will fairly reliably strip most HTML tags:
import re
re.sub('<[^<]+?>', '', text)
For those that don't understand regex, this searches for a string <...>
, where the inner content is made of one or more (+
) characters that isn't a <
. The ?
means that it will match the smallest string it can find. For example given <p>Hello</p>
, it will match <'p>
and </p>
separately with the ?
. Without it, it will match the entire string <..Hello..>
.
If non-tag <
appears in html (eg. 2 < 3
), it should be written as an escape sequence &...
anyway so the ^<
may be unnecessary.
I always used this function to strip HTML tags, as it requires only the Python stdlib:
For Python 3:
from io import StringIO
from html.parser import HTMLParser
class MLStripper(HTMLParser):
def __init__(self):
super().__init__()
self.reset()
self.strict = False
self.convert_charrefs= True
self.text = StringIO()
def handle_data(self, d):
self.text.write(d)
def get_data(self):
return self.text.getvalue()
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()
For Python 2:
from HTMLParser import HTMLParser
from StringIO import StringIO
class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.text = StringIO()
def handle_data(self, d):
self.text.write(d)
def get_data(self):
return self.text.getvalue()
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()