Scrapy, a fast high-level web crawling & scraping framework for Python.
ignore meta-refresh redirect when embedded inside <noscript> tag. closes issue 18
Pablo Hoffmanec1ef0235f9d
diff --git a/scrapy/tests/test_utils_response.py b/scrapy/tests/test_utils_response.pyindex 6cde6332c..9e75a7c7f 100644--- a/scrapy/tests/test_utils_response.py+++ b/scrapy/tests/test_utils_response.py@@ -3,7 +3,8 @@ import unittestimport urlparsefrom scrapy.http import Response, TextResponse, HtmlResponse-from scrapy.utils.response import body_or_str, response_httprepr, open_in_browser+from scrapy.utils.response import body_or_str, response_httprepr, open_in_browser, \+ get_meta_refresh__doctests__ = ['scrapy.utils.response']@@ -55,5 +56,21 @@ class ResponseUtilsTest(unittest.TestCase):self.assertRaises(TypeError, open_in_browser, Response(url, body=body), \debug=True)+ def test_get_meta_refresh(self):+ r1 = HtmlResponse("http://www.example.com", body="""+ <html>+ <head><title>Dummy</title><meta http-equiv="refresh" content="5;url=http://example.org/newpage" /></head>+ <body>blahablsdfsal&</body>+ </html>""")+ r2 = HtmlResponse("http://www.example.com", body="""+ <html>+ <head><title>Dummy</title><noScript>+ <meta http-equiv="refresh" content="5;url=http://example.org/newpage" /></head>+ </noSCRIPT>+ <body>blahablsdfsal&</body>+ </html>""")+ self.assertEqual(get_meta_refresh(r1), (5.0, 'http://example.org/newpage'))+ self.assertEqual(get_meta_refresh(r2), (None, None))+if __name__ == "__main__":unittest.main()diff --git a/scrapy/utils/response.py b/scrapy/utils/response.pyindex 668e344f0..daa3a62bc 100644--- a/scrapy/utils/response.py+++ b/scrapy/utils/response.py@@ -4,6 +4,7 @@ scrapy.http.Response objects"""import os+import reimport weakrefimport webbrowserimport tempfile@@ -12,7 +13,6 @@ from twisted.web import httpfrom twisted.web.http import RESPONSESfrom w3lib import html-from scrapy.xlib.BeautifulSoup import BeautifulSoupfrom scrapy.http import Response, HtmlResponsedef body_or_str(obj, unicode=True):@@ -34,11 +34,13 @@ def get_base_url(response):response.encoding)return _baseurl_cache[response]+_noscript_re = re.compile(u'<noscript>.*?</noscript>', re.IGNORECASE | re.DOTALL)_metaref_cache = weakref.WeakKeyDictionary()def get_meta_refresh(response):"""Parse the http-equiv refrsh parameter from the given response"""if response not in _metaref_cache:text = response.body_as_unicode()[0:4096]+ text = _noscript_re.sub(u'', text)_metaref_cache[response] = html.get_meta_refresh(text, response.url, \response.encoding)return _metaref_cache[response]