Last active
December 26, 2017 07:25
-
-
Save hirokts/ccf0a0db60ac11021f12445940494430 to your computer and use it in GitHub Desktop.
HTMLParserでうまくコメントが除去されない(Python3.6)
This file contains 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 MLStripper(HTMLParser): | |
def __init__(self): | |
self.reset() | |
self.strict = False | |
self.convert_charrefs= True | |
self.fed = [] | |
def handle_data(self, d): | |
# ここのif文は本来なくていいはずなんだけど、<style>などのあとに改行で<!--コメントがあると | |
# <style>除去のあと改行から始まる<!--がdataとして扱われ、それがコメント扱いにならない | |
# どうすればいいんでしょうか | |
if not re.match(r'[\n\s]*<!--', 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