Cookies

Scrapy keeps track of the cookies that websites set and sends them back on later requests to those websites, just like a web browser does. That is the job of CookiesMiddleware, which is enabled by default.

Setting cookies on a request

Use the cookies parameter of Request to send cookies of your own, either as a dict:

request = Request(
    url="https://example.com",
    cookies={"currency": "USD", "country": "UY"},
)

Or as a list of dicts, which also lets you set cookie attributes:

request = Request(
    url="https://example.com",
    cookies=[
        {
            "name": "currency",
            "value": "USD",
            "domain": "example.com",
            "path": "/currency",
            "secure": True,
        },
    ],
)

Setting attributes is only useful if the cookies are stored for later requests, i.e. if dont_merge_cookies is not enabled.

Caution

Cookies set through the Cookie header are not handled by CookiesMiddleware, which drops that header.

Caution

When a cookie name or value is a byte sequence that is not UTF-8 encoded, the cookie is dropped and a warning is logged. See Advanced customization to customize the logging behavior.

COOKIES_ENABLED

Default: True

Whether to enable CookiesMiddleware. If disabled, no cookies are sent to web servers.

COOKIES_DEBUG

Default: False

If enabled, Scrapy logs all cookies sent in requests (i.e. the Cookie header) and all cookies received in responses (i.e. the Set-Cookie header):

2011-04-06 14:35:10-0300 [scrapy.core.engine] INFO: Spider opened
2011-04-06 14:35:10-0300 [scrapy.downloadermiddlewares.cookies] DEBUG: Sending cookies to: <GET http://www.diningcity.com/netherlands/index.html>
        Cookie: clientlanguage_nl=en_EN
2011-04-06 14:35:14-0300 [scrapy.downloadermiddlewares.cookies] DEBUG: Received cookies from: <200 http://www.diningcity.com/netherlands/index.html>
        Set-Cookie: JSESSIONID=B~FA4DC0C496C8762AE4F1A620EAB34F38; Path=/
        Set-Cookie: ip_isocode=US
        Set-Cookie: clientlanguage_nl=en_EN; Expires=Thu, 07-Apr-2011 21:21:34 GMT; Path=/
2011-04-06 14:49:50-0300 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://www.diningcity.com/netherlands/index.html> (referer: None)
[...]

CookiesMiddleware

class scrapy.downloadermiddlewares.cookies.CookiesMiddleware(debug: bool = False)[source]

This middleware enables working with sites that need cookies