How to keep cookies on request retry? #2028
Replies: 2 comments
-
|
Cookies aren't being lost, they're stored on the session, just not on the one being used for the retry. The catch is that your request isn't pinned to a specific session. When the handler raises and the request is retried, the crawler picks a new random session from the pool. Since the default There are two ways to fix this, depending on whether you still want session rotation. 1. Use a session pool with a single sessionThis is the simplest option and keeps your current raise-to-retry flow: crawler = HttpCrawler(
session_pool=SessionPool(max_pool_size=1),
# ...
)2. Pin the request to the current sessionIf you still want session rotation for everything else, don't raise to trigger the retry. Instead, re-enqueue the same URL and bind it to the session that now has the cookies: from crawlee import Request
@crawler.router.default_handler
async def handler(context):
if context.request.user_data.get('prepared'):
# Do the work - cookies from the setup step are on this session.
return
# First pass: establish cookies.
await context.send_request('https://.../set-something')
# Re-enqueue the same URL, pinned to the same session.
await context.add_requests([
Request.from_url(
context.request.url,
session_id=context.session.id, # Pins the retry to this session.
user_data={'prepared': True},
always_enqueue=True, # Bypasses deduplication so the same URL runs again.
)
])Cookies are scoped by domain, so make sure the I'm aware that this isn't the most ergonomic or intuitive way to reuse sessions. We expect to rethink and redesign this in the upcoming Crawlee JS v4 and Crawlee Python v2, probably later this year. |
Beta Was this translation helpful? Give feedback.
-
|
I was printing I would prefer to raise an exception and retry, because with adding requests it could go in an infinite loop of adding requests when failing. For this way, is it OK to set cookies into some other global variable after using |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a workflow that is something like this: call the URL and check for something first. If it's not right, then use
send_requestto set something and raise an exception which triggers a retry on the request. I'm printing out cookies and after usingsend_requestthey are there, but on a retry they aren't there anymore.I have
use_session_pool=Truein crawler init, I'm printingcontext.session.idand it shows the same number so it should be working on the same session.So is there a way to keep cookies no matter what basically?
Beta Was this translation helpful? Give feedback.
All reactions