Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Build selectors from bytes only with parsel 1.12.0+, fixing UTF-16 (#8222)

Adrian9d1085dd46f5
diff --git a/scrapy/selector/unified.py b/scrapy/selector/unified.py
index 5e8242ea6..69344814b 100644
--- a/scrapy/selector/unified.py
+++ b/scrapy/selector/unified.py
@@ -5,6 +5,7 @@ from typing import Any, Literal
from parsel import Selector as _ParselSelector
from scrapy.http import HtmlResponse, JsonResponse, TextResponse, XmlResponse
+from scrapy.utils._deps_compat import PARSEL_BODY_ENCODING_FIXED
from scrapy.utils.python import to_bytes
from scrapy.utils.response import get_base_url
from scrapy.utils.trackref import object_ref
@@ -102,7 +103,7 @@ class Selector(_ParselSelector, object_ref):
if response is not None:
# parsel only treats bytes and text equivalently for "html" and
# "xml", and treats an empty body as absent, unlike empty text.
- if type in ("html", "xml") and response.body:
+ if PARSEL_BODY_ENCODING_FIXED and type in ("html", "xml") and response.body:
encoding = response.encoding
# None means the encoding was declared, not inferred from
# the body, so the body hasn't been decoded as a side effect.
diff --git a/scrapy/utils/_deps_compat.py b/scrapy/utils/_deps_compat.py
index e96f0c417..f76e92c79 100644
--- a/scrapy/utils/_deps_compat.py
+++ b/scrapy/utils/_deps_compat.py
@@ -2,6 +2,7 @@ import sys
from OpenSSL import __version__ as PYOPENSSL_VERSION_STRING
from packaging.version import Version
+from parsel import __version__ as PARSEL_VERSION_STRING
from twisted import version as TWISTED_VERSION
from twisted.python.versions import Version as TxVersion
@@ -16,6 +17,10 @@ TWISTED_TLS_NEW_IMPL = TWISTED_VERSION >= TxVersion("twisted", 26, 4, 0)
# lowerMaximumSecurityTo off-by-1, https://github.com/twisted/twisted/issues/10232
TWISTED_TLS_LIMITS_OFFBY1 = TWISTED_VERSION < TxVersion("twisted", 26, 4, 0)
+PARSEL_VERSION = Version(PARSEL_VERSION_STRING)
+# body decoding fixes for non-UTF-8 encodings, https://github.com/scrapy/parsel/pull/356
+PARSEL_BODY_ENCODING_FIXED = PARSEL_VERSION >= Version("1.12.0")
+
PYOPENSSL_VERSION = Version(PYOPENSSL_VERSION_STRING)
# SSL.Context.set_cipher_list() creates a temporary connection, making the context immutable
PYOPENSSL_SET_CIPHER_LIST_TMP_CONN = PYOPENSSL_VERSION < Version("25.2.0")
diff --git a/tests/test_selector.py b/tests/test_selector.py
index 77799ad02..482ca8407 100644
--- a/tests/test_selector.py
+++ b/tests/test_selector.py
@@ -108,6 +108,20 @@ class TestSelector:
)
Selector(r1).xpath("//text()").getall()
+ @pytest.mark.parametrize(
+ ("body", "headers"),
+ [
+ ("<p>£€</p>".encode("utf-16-le"), {}),
+ (
+ "<p>£€</p>".encode("utf-16-le"),
+ {"Content-Type": "text/html; charset=utf-16-le"},
+ ),
+ ],
+ )
+ def test_utf16(self, body: bytes, headers: dict[str, str]) -> None:
+ response = HtmlResponse("https://example.com", body=body, headers=headers)
+ assert response.css("p::text").get() == "£€"
+
def test_weakref_slots(self):
"""Check that classes are using slots and are weak-referenceable"""
x = Selector(text="")