importreFIND_SCRIPT_TAGS=re.compile(r'(<script .*?>.*?</script>)',re.DOTALL)classScriptsAtBottomMiddleware(object):"""Finds all tags <script> at the HTML response and move to the end of the document, before tag </html>. This makes the page load faster, because <script> blocks parallel loading. Read more about this at: - http://developer.yahoo.com/performance/rules.html#js_bottom - http://code.google.com/intl/pt-BR/speed/page-speed/docs/rtt.html#PutStylesBeforeScripts """defprocess_response(self,request,response):ifresponse['content-type'][:9]=='text/html':# Find scripts tags with srcf=FIND_SCRIPT_TAGS.findall(response.content)# Remove the tags foundfortaginf:response.content=response.content.replace(tag,'')# Insert the tags found at the bottompos=response.content.find('</html>')response.content=response.content[:pos]+'\n'.join(f)+'\n'+response.content[pos:]returnresponse