Created
November 24, 2020 14:38
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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