question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose cookiejars

See original GitHub issue

Scrapy cookiejar API is limited:

I think we should provide a better API for ‘sessions’. It should allow to

  1. access current session cookies;
  2. ‘fork’ a session - start separate sessions from the current session.

Currently I’m using an ugly hack to access cookies:

class ExposeCookiesMiddleware(CookiesMiddleware):
    """
    This middleware appends CookieJar with current cookies to response flags.

    To use it, disable default CookiesMiddleware and enable
    this middleware instead::

        DOWNLOADER_MIDDLEWARES = {
            'scrapy.downloadermiddlewares.cookies.CookiesMiddleware': None,
            'autologin.middleware.ExposeCookiesMiddleware': 700,
        }

    """
    def process_response(self, request, response, spider):
        response = super(ExposeCookiesMiddleware, self).process_response(
            request, response, spider)
        cookiejarkey = request.meta.get("cookiejar")
        response.flags.append(self.jars[cookiejarkey])
        return response


def get_cookiejar(response):
    for obj in response.flags:
        if isinstance(obj, CookieJar):
            return obj

I don’t have a concrete API proposal, but likely it should use a word ‘session’ 😃

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:24
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
pawelmhmcommented, Mar 24, 2016

getting and setting cookies in Scrapy is really huge pain so big 👍 from me.

In project I’m working on now we use following solution that sets “jars” from Cookie Middleware on spider, and then allows you to use it.


class CustomCookiesMiddleware(cookies.CookiesMiddleware):
    @classmethod
    def from_crawler(cls, crawler):
        o = super(CustomCookiesMiddleware, cls).from_crawler(crawler)
        crawler.signals.connect(o.spider_opened, signal=signals.spider_opened)
        return o

    def spider_opened(self, spider):
        self.enabled = getattr(spider, 'cookies_enabled', self.enabled)
        spider._cookiejars = self.jars
def BaseSpider(Spider):
    def get_cookie(self, name, cookiejar=None):
           if cookiejar not in self._cookiejars:
                raise KeyError(u'cookiejar {} does not exist'.format(cookiejar))
           _dict = {c.name: c.value for c in self._cookiejars[cookiejar]}
            return _dict.get(name)

but this is just for getting cookies, we dont have anything for setting cookies, we should definitely add something, last time I had to replace cookie value I had to write ugly code like this

locale_cookie = self._cookiejars[None]._cookies[".xbox.com"]["/"].get("defCulture")
locale_cookie.value = self.locale
0reactions
kmikecommented, Jun 25, 2019

https://github.com/scrapy/scrapy/pull/3563#issuecomment-505105007 has yet another syntax proposal (haven’t thought about it in depth though).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exposed | Jar, Plexus products, Cookie jars
Push Away, Cookie Jars, Plexus Products, Dollar Stores, Aura, Exposed,. Rachel Marie Wolfe Ammann. 51 followers. More information. Push Away · Cookie Jars....
Read more >
requests.cookies — Requests 2.28.1 documentation
CookieJar, but exposes a dict interface. This is the CookieJar we create by default for requests and sessions that don't specify one, since...
Read more >
python - Putting a `Cookie` in a `CookieJar`
I'm using the Python Requests library to make HTTP requests. I obtain a cookie from the server as text. How do I turn...
Read more >
CookieJar
Middleware Constructor Using a Provided CookieJar. ... Constructor which builds a non-exposed CookieJar and applies it to the client.
Read more >
Issue 23498: Expose http.cookiejar.split_header_words()
Title: Expose http.cookiejar.split_header_words(). Type: enhancement, Stage: Components: Library (Lib), Versions: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found