MCPcopy Create free account
hub / github.com/apify/crawlee-python / handler

Function handler

tests/unit/crawlers/_playwright/test_playwright_crawler.py:382–407  ·  view source on GitHub ↗
(context: PlaywrightCrawlingContext)

Source from the content-addressed store, hash-verified

380
381 @crawler.router.default_handler
382 async def handler(context: PlaywrightCrawlingContext) -> None:
383 if not context.session:
384 return
385
386 sessions_ids.append(context.session.id)
387 sessions[context.session.id] = context.session
388 response_data = json.loads(await context.response.text())
389 response_cookies[context.session.id] = response_data.get('cookies')
390
391 if context.request.unique_key == '1':
392 # With the second request, we check the cookies in the session and set retire
393 await context.add_requests(
394 [
395 Request.from_url(
396 str(server_url.with_path('/cookies')), unique_key='2', user_data={'retire_session': True}
397 )
398 ]
399 )
400 return
401
402 if context.request.user_data.get('retire_session'):
403 context.session.retire()
404
405 if context.request.unique_key == '2':
406 # The third request is made with a new session to make sure it does not use another session's cookies
407 await context.add_requests([Request.from_url(str(server_url.with_path('/cookies')), unique_key='3')])
408
409 await crawler.run(
410 [

Callers

nothing calls this directly

Calls 5

from_urlMethod · 0.80
retireMethod · 0.80
enqueue_linksMethod · 0.80
add_requestsMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected