Skip to main content
Prerequisites
  1. You’ve read the Getting started page and know how to create and run a basic spider.
  2. You’re familiar with Fetchers basics and the differences between HTTP, Dynamic, and Stealthy sessions.
A spider can use multiple fetcher sessions simultaneously — for example, a fast HTTP session for simple pages and a stealth browser session for protected pages. This page shows you how to configure and use sessions.

What are Sessions?

A session is a pre-configured fetcher instance that stays alive for the duration of the crawl. Instead of creating a new connection or browser for every request, the spider reuses sessions, which is faster and more resource-efficient. By default, every spider creates a single FetcherSession. You can add more sessions or swap the default by overriding the configure_sessions() method. You must use the async version of each session only:

Configuring Sessions

Override configure_sessions() on your spider to set up sessions. The manager parameter is a SessionManager instance — use manager.add() to register sessions:

SessionManager.add() Parameters

The manager.add() method takes:
Important Notes:
  1. If you don’t specify which session to use in requests, the default session is used. The default session is determined in one of two ways:
    • The first session you add becomes the default automatically
    • The session that gets default=True when added to the manager
  2. The session instances you pass don’t have to be already started — the spider checks all sessions and starts them automatically.
  3. If you want a specific session to start only when used, use the lazy argument when adding that session to the manager. Example: start the browser only when you need it, not with the spider start.

SessionManager Implementation

The session management logic is implemented in session.py:
session.py:83-92
Lazy sessions are started on first use:
session.py:101-130

Multi-Session Spider

Here’s a practical example: use a fast HTTP session for listing pages and a stealth browser for detail pages that have bot protection:
The key is the sid parameter — it tells the spider which session to use for each request. When you call response.follow() without sid, the session ID from the original request is inherited.

Same Session Class, Different Configurations

Sessions don’t have to be from different classes — you can use the same session class with different configurations:
This approach lets you separate concerns and maintain different cookies/state for specific request types.

Session Arguments

Extra keyword arguments passed to a Request (or through response.follow(**kwargs)) are forwarded to the session’s fetch method. This lets you customize individual requests without changing the session configuration:
Normally, when you use FetcherSession, Fetcher, or AsyncFetcher, you specify the HTTP method with the corresponding method like .get() and .post(). But while using FetcherSession in spiders, you can’t do this. By default, the request is an HTTP GET request; if you want to use another HTTP method, you have to pass it to the method argument, as in the above example. The reason is to unify the Request interface across all session types.

Browser-Specific Arguments

For browser sessions (AsyncDynamicSession, AsyncStealthySession), you can pass browser-specific arguments like wait_selector, page_action, or extra_headers:

Argument Inheritance

Session arguments (**kwargs) passed from the original request are inherited by response.follow(). New kwargs take precedence over inherited ones.

Session Lifecycle

Upon spider closure, the manager automatically checks whether any sessions are still running and closes them before closing the spider.
The cleanup logic is implemented in the SessionManager:
session.py:94-99