BOM should take precedence over Content-Type header when detecting the encoding closes #5601

This commit is contained in:
Mohammadtaher Abbasi 2022-08-28 20:28:13 +04:30
parent 52d93490f5
commit e411ea94eb
1 changed files with 12 additions and 2 deletions

View File

@ -11,8 +11,13 @@ from typing import Generator, Tuple
from urllib.parse import urljoin
import parsel
from w3lib.encoding import (html_body_declared_encoding, html_to_unicode,
http_content_type_encoding, resolve_encoding)
from w3lib.encoding import (
html_body_declared_encoding,
html_to_unicode,
http_content_type_encoding,
resolve_encoding,
read_bom,
)
from w3lib.html import strip_html5_whitespace
from scrapy.http import Request
@ -60,6 +65,7 @@ class TextResponse(Response):
def _declared_encoding(self):
return (
self._encoding
or self._bom_encoding()
or self._headers_encoding()
or self._body_declared_encoding()
)
@ -117,6 +123,10 @@ class TextResponse(Response):
def _body_declared_encoding(self):
return html_body_declared_encoding(self.body)
@memoizemethod_noargs
def _bom_encoding(self):
return read_bom(self.body)[0]
@property
def selector(self):
from scrapy.selector import Selector