Skip to content

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

Deprecate Response.body_as_unicode() (#4555)

nsirletticb8140a42a27
diff --git a/docs/topics/request-response.rst b/docs/topics/request-response.rst
index 024f46466..15a83f453 100644
--- a/docs/topics/request-response.rst
+++ b/docs/topics/request-response.rst
@@ -834,11 +834,6 @@ TextResponse objects
.. automethod:: TextResponse.follow_all
- .. method:: TextResponse.body_as_unicode()
-
- The same as :attr:`text`, but available as a method. This method is
- kept for backward compatibility; please prefer ``response.text``.
-
HtmlResponse objects
--------------------
diff --git a/scrapy/http/response/text.py b/scrapy/http/response/text.py
index 2f0f3820c..5614e6e55 100644
--- a/scrapy/http/response/text.py
+++ b/scrapy/http/response/text.py
@@ -5,6 +5,7 @@ discovering (through HTTP headers) to base Response class.
See documentation in docs/topics/request-response.rst
"""
+import warnings
from contextlib import suppress
from typing import Generator
from urllib.parse import urljoin
@@ -14,6 +15,7 @@ from w3lib.encoding import (html_body_declared_encoding, html_to_unicode,
http_content_type_encoding, resolve_encoding)
from w3lib.html import strip_html5_whitespace
+from scrapy.exceptions import ScrapyDeprecationWarning
from scrapy.http import Request
from scrapy.http.response import Response
from scrapy.utils.python import memoizemethod_noargs, to_unicode
@@ -61,6 +63,9 @@ class TextResponse(Response):
def body_as_unicode(self):
"""Return body as unicode"""
+ warnings.warn('Response.body_as_unicode() is deprecated, '
+ 'please use Response.text instead.',
+ ScrapyDeprecationWarning)
return self.text
@property
diff --git a/tests/test_http_response.py b/tests/test_http_response.py
index 43d6d936a..2f73afe56 100644
--- a/tests/test_http_response.py
+++ b/tests/test_http_response.py
@@ -1,7 +1,9 @@
import unittest
+from warnings import catch_warnings
from w3lib.encoding import resolve_encoding
+from scrapy.exceptions import ScrapyDeprecationWarning
from scrapy.http import (Request, Response, TextResponse, HtmlResponse,
XmlResponse, Headers)
from scrapy.selector import Selector
@@ -660,6 +662,13 @@ class TextResponseTest(BaseResponseTest):
with self.assertRaises(ValueError):
response.follow_all(css='a[href*="example.com"]', xpath='//a[contains(@href, "example.com")]')
+ def test_body_as_unicode_deprecation_warning(self):
+ with catch_warnings(record=True) as warnings:
+ r1 = self.response_class("http://www.example.com", body=u'Hello', encoding='utf-8')
+ self.assertEqual(r1.body_as_unicode(), u'Hello')
+ self.assertEqual(len(warnings), 1)
+ self.assertEqual(warnings[0].category, ScrapyDeprecationWarning)
+
class HtmlResponseTest(TextResponseTest):