Skip to content

Instantly share code, notes, and snippets.

@prettyirrelevant
Created November 24, 2020 14:38
Show Gist options
  • Save prettyirrelevant/e065c1ff54e869814dca1bae4d3d607c to your computer and use it in GitHub Desktop.
Save prettyirrelevant/e065c1ff54e869814dca1bae4d3d607c to your computer and use it in GitHub Desktop.
a python script to clean an input of html and return just text
class ParseHTML(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):
# convert entities to utf-8
parser = HTMLParser()
html = parser.unescape(html)
s = ParseHTML()
s.feed(html)
return s.get_data()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment