Created
February 27, 2015 17:39
-
-
Save dmiro/d5c6738ed11c7dfc8495 to your computer and use it in GitHub Desktop.
HTML2Text
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
| def HTML2Text(html): | |
| class _HTMLParser(HTMLParser): | |
| def __init__(self): | |
| HTMLParser.__init__(self) | |
| self._text = [] | |
| def handle_data(self, data): | |
| append = True | |
| text = data.split() | |
| if text: | |
| tag = self.get_starttag_text() | |
| if tag: | |
| tag = tag.lower() | |
| append = not tag.startswith(('<script','<style')) | |
| if append: | |
| self._text.extend(text) | |
| h = _HTMLParser() | |
| h.feed(html) | |
| return ' '.join(h._text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment