Expose cookiejars
See original GitHub issueScrapy cookiejar API is limited:
- meta key is called
cookiejar
, but you can’t put CookieJar object there, in fact it meanscookiejar_id
orsession_id
, notcookiejar
; this is confusing. It should have been calledsession_id
IMHO. - there is no way to get or set current cookies; it is a popular issue we don’t have a solution for (see http://stackoverflow.com/questions/8708346/access-session-cookie-in-scrapy-spiders and https://github.com/scrapy/scrapy/issues/1448).
I think we should provide a better API for ‘sessions’. It should allow to
- access current session cookies;
- ‘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:
- Created 7 years ago
- Reactions:24
- Comments:9 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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
https://github.com/scrapy/scrapy/pull/3563#issuecomment-505105007 has yet another syntax proposal (haven’t thought about it in depth though).