Last active
October 22, 2015 07:32
-
-
Save jinstrive/5d5ed44226fbd61e204e to your computer and use it in GitHub Desktop.
remove html tags
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
from HTMLParser import HTMLParser | |
def remove_html_tags(html): | |
""" | |
移除html 标签类 方法 | |
""" | |
if not html: | |
return html | |
if not isinstance(html, basestring): | |
return html | |
move_html_parser = MHTParser() | |
move_html_parser.feed(html) | |
ret = move_html_parser.get_data() | |
del move_html_parser | |
return ret | |
class MHTParser(HTMLParser): | |
""" | |
移除html标签类 帮助类 | |
""" | |
def __init__(self): | |
self.reset() | |
self.fed = [] | |
def handle_data(self, d): | |
self.fed.append(d) | |
def get_data(self): | |
return ''.join(self.fed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment