Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookiejars exposed #6218

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ Does Scrapy manage cookies automatically?
Yes, Scrapy receives and keeps track of cookies sent by servers, and sends them
back on subsequent requests, like any regular web browser does.

For more info see :ref:`topics-request-response` and :ref:`cookies-mw`.
For more info see :ref:`topics-request-response`, :ref:`cookies-mw` and :ref:`cookiejars`.

How can I see the cookies being sent and received from Scrapy?
--------------------------------------------------------------
Expand Down
48 changes: 48 additions & 0 deletions docs/topics/downloader-middleware.rst
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,54 @@ Here's an example of a log with :setting:`COOKIES_DEBUG` enabled::
[...]


.. _cookiejars:

Direct access to cookiejars from spider
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this section could go before COOKIES_ENABLED?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about this. Obviously COOKIES_ENABLED is more important than this, then as I think it should be after as it placed now

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In some cases it is required to directly set specific values in an existing
cookiejar.

.. code-block:: python

import scrapy
from scrapy.crawler import CrawlerProcess


class Quotes(scrapy.Spider):
name = "quotes"
custom_settings = {"DOWNLOAD_DELAY": 1}

def start_requests(self):
yield scrapy.Request(
url="https://quotes.toscrape.com/login", callback=self.login
)

def login(self, response):
self.logger.info(self.cookie_jars[None]) # scrapy.http.cookies.CookieJar object
self.logger.info(self.cookie_jars[None].jar) # http.cookiejar object

locale_cookie = (
self.cookie_jars[None]._cookies["quotes.toscrape.com"]["/"].get("session")
)
locale_cookie.value = locale_cookie.value.upper()
self.logger.info(self.cookie_jars[None].jar)


if __name__ == "__main__":
p = CrawlerProcess()
p.crawl(Quotes)
p.start()

Log output::

2024-02-23 10:51:27 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://quotes.toscrape.com/login> (referer: None)
2024-02-23 10:51:27 [quotes] INFO: <scrapy.http.cookies.CookieJar object at 0x00000217DB719B40>
2024-02-23 10:51:27 [quotes] INFO: <CookieJar[<Cookie session=eyJjc3JmX3Rva2VuIjoiSnFQQU9GTGt1amZzZ3J3UVdHeGV6WFR2UnBpY0Job1NWS3liWmxhblVISXROREVtQ2RZTSJ9.Zdhqng.8uQzjuvDfOcNJHV7luY5Na6C1N0 for quotes.toscrape.com/>]>
2024-02-23 10:51:27 [quotes] INFO: <CookieJar[<Cookie session=EYJJC3JMX3RVA2VUIJOISNFQQU9GTGT1AMZZZ3J3UVDHEGV6WFR2UNBPY0JOB1NWS3LIWMXHBLVISXROREVTQ2RZTSJ9.ZDHQNG.8UQZJUVDFOCNJHV7LUY5NA6C1N0 for quotes.toscrape.com/>]>
2024-02-23 10:51:27 [scrapy.core.engine] INFO: Closing spider (finished)


DefaultHeadersMiddleware
------------------------

Expand Down
9 changes: 7 additions & 2 deletions scrapy/downloadermiddlewares/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from tldextract import TLDExtract

from scrapy import Request, Spider
from scrapy import Request, Spider, signals
from scrapy.crawler import Crawler
from scrapy.exceptions import NotConfigured
from scrapy.http import Response
Expand Down Expand Up @@ -51,7 +51,12 @@ def __init__(self, debug: bool = False):
def from_crawler(cls, crawler: Crawler) -> Self:
if not crawler.settings.getbool("COOKIES_ENABLED"):
raise NotConfigured
return cls(crawler.settings.getbool("COOKIES_DEBUG"))
o = cls(crawler.settings.getbool("COOKIES_DEBUG"))
crawler.signals.connect(o.spider_opened, signal=signals.spider_opened)
return o

def spider_opened(self, spider: Spider) -> None:
spider.cookie_jars = self.jars

def _process_cookies(
self, cookies: Iterable[Cookie], *, jar: CookieJar, request: Request
Expand Down