Scrapy, a fast high-level web crawling & scraping framework for Python.
improved encoding detection by adding support for HTML5 meta charset
Pablo Hoffman4bb409923cc3
diff --git a/scrapy/http/response/html.py b/scrapy/http/response/html.pyindex dc812ac0e..1ef52490c 100644--- a/scrapy/http/response/html.py+++ b/scrapy/http/response/html.py@@ -18,15 +18,15 @@ class HtmlResponse(TextResponse):_httpequiv_re = _template % ('http-equiv', 'Content-Type')_content_re = _template % ('content', r'(?P<mime>[^;]+);\s*charset=(?P<charset>[\w-]+)')- _encoding_re = _template % ('encoding', r'(?P<charset>[\w-]+)')+ _content2_re = _template % ('charset', r'(?P<charset>[\w-]+)')METATAG_RE = re.compile(r'<meta\s+%s\s+%s' % (_httpequiv_re, _content_re), re.I)- METATAG_RE2 = re.compile(r'<meta\s+%s\s+%s' % (_content_re, _httpequiv_re), re.I)+ METATAG2_RE = re.compile(r'<meta\s+%s' % _content2_re, re.I)@memoizemethod_noargsdef _body_declared_encoding(self):chunk = self.body[:5000]- match = self.METATAG_RE.search(chunk) or self.METATAG_RE2.search(chunk)+ match = self.METATAG_RE.search(chunk) or self.METATAG2_RE.search(chunk)return match.group('charset') if match else Nonediff --git a/scrapy/tests/test_http_response.py b/scrapy/tests/test_http_response.pyindex 3c6afdcf9..b1edf4a3a 100644--- a/scrapy/tests/test_http_response.py+++ b/scrapy/tests/test_http_response.py@@ -260,6 +260,10 @@ class HtmlResponseTest(TextResponseTest):r4 = r3.replace(body=body)self._assert_response_values(r4, 'iso-8859-1', body)+ def test_html5_meta_charset(self):+ body = """<html><head><meta charset="gb2312" /><title>Some page</title><body>bla bla</body>"""+ r1 = self.response_class("http://www.example.com", body=body)+ self._assert_response_values(r1, 'gb2312', body)class XmlResponseTest(TextResponseTest):