Skip to content

Instantly share code, notes, and snippets.

@hirokts
Last active December 26, 2017 07:25
Show Gist options
  • Save hirokts/ccf0a0db60ac11021f12445940494430 to your computer and use it in GitHub Desktop.
Save hirokts/ccf0a0db60ac11021f12445940494430 to your computer and use it in GitHub Desktop.
HTMLParserでうまくコメントが除去されない(Python3.6)
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