import re class StripHtmlCommentsMiddleware: """ Strips all html comments from response content. """ def __init__(self): self.htmlcomments = re.compile('\') def process_response(self, request, response): if ("text" in response['Content-Type']): new_content = self.htmlcomments.sub('', response.content) response.content = new_content return response else: return response