Skip to content

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

Implement Response.cb_kwargs

Adrián Chavesdf937d8280fe
diff --git a/docs/topics/request-response.rst b/docs/topics/request-response.rst
index 4cf367d96..05b7bb5c7 100644
--- a/docs/topics/request-response.rst
+++ b/docs/topics/request-response.rst
@@ -672,6 +672,18 @@ Response objects
.. seealso:: :attr:`Request.meta` attribute
+ .. attribute:: Response.cb_kwargs
+
+ A shortcut to the :attr:`Request.cb_kwargs` attribute of the
+ :attr:`Response.request` object (ie. ``self.request.cb_kwargs``).
+
+ Unlike the :attr:`Response.request` attribute, the
+ :attr:`Response.cb_kwargs` attribute is propagated along redirects and
+ retries, so you will get the original :attr:`Request.cb_kwargs` sent
+ from your spider.
+
+ .. seealso:: :attr:`Request.cb_kwargs` attribute
+
.. attribute:: Response.flags
A list that contains flags for this response. Flags are labels used for
diff --git a/scrapy/http/response/__init__.py b/scrapy/http/response/__init__.py
index 64e9c6c20..ee9720d52 100644
--- a/scrapy/http/response/__init__.py
+++ b/scrapy/http/response/__init__.py
@@ -24,6 +24,16 @@ class Response(object_ref):
self.request = request
self.flags = [] if flags is None else list(flags)
+ @property
+ def cb_kwargs(self):
+ try:
+ return self.request.cb_kwargs
+ except AttributeError:
+ raise AttributeError(
+ "Response.cb_kwargs not available, this response "
+ "is not tied to any request"
+ )
+
@property
def meta(self):
try: