FetcherSession, StealthySession, and DynamicSession.
Session Types Overview
| Session | Fetcher | Use Case | Maintains |
|---|---|---|---|
| FetcherSession | Fetcher | HTTP requests | Cookies, connections |
| StealthySession | StealthyFetcher | Anti-bot bypass | Browser context, cookies |
| DynamicSession | DynamicFetcher | Browser automation | Browser context, cookies |
FetcherSession (HTTP)
Fast HTTP sessions with browser impersonation.Basic Usage
Configuration Options
Async Context Support
FetcherSession is context-aware and works in both sync and async:Per-Request Overrides
StealthySession (Anti-Bot)
Persistent browser session with anti-bot capabilities.Basic Usage
Configuration Options
Persistent Browser Profile
Maintain browser state across script runs:Async Stealth Session
DynamicSession (Browser Automation)
Persistent browser session for automation.Basic Usage
Configuration Options
Async Dynamic Session
Session Lifecycle
Context Manager (Recommended)
Always use context managers to ensure proper cleanup:Manual Lifecycle
For advanced use cases:Page Pooling (Browser Sessions)
Browser sessions (Stealthy and Dynamic) support page pooling for concurrent requests:Pool Configuration
Pool Stats
Monitor pool usage:Per-Request Customization
Override session settings per request:Cookie Management
Setting Cookies
Accessing Cookies
Browser sessions provide access to cookies through the page object:Best Practices
Always use context managers
Always use context managers
Use
with statements to ensure proper cleanup of sessions, especially for browser sessions.Reuse sessions for multiple requests
Reuse sessions for multiple requests
Sessions maintain state and connections. Reuse them instead of creating new ones for each request.
Configure defaults wisely
Configure defaults wisely
Set sensible defaults at the session level and override only when needed per-request.
Use persistent profiles for long-term state
Use persistent profiles for long-term state
For browser sessions, use
user_data_dir to maintain state across script runs.Tune page pool size for async
Tune page pool size for async
Set
max_pages based on your concurrency needs and system resources. More tabs = more memory.Monitor pool stats in async
Monitor pool stats in async
Use
get_pool_stats() to debug concurrency issues and optimize pool size.Session Comparison
| Feature | FetcherSession | StealthySession | DynamicSession |
|---|---|---|---|
| Speed | ⚡⚡⚡ | ⚡⚡ | ⚡ |
| Connection Pooling | ✅ | ❌ | ❌ |
| Browser Context | ❌ | ✅ | ✅ |
| JavaScript | ❌ | ✅ | ✅ |
| Cloudflare Bypass | ❌ | ✅ | ❌ |
| Page Pool | ❌ | ✅ | ✅ |
| Resource Usage | Low | Medium | High |
| Persistent Profile | ❌ | ✅ | ✅ |
Next Steps
Proxy Rotation
Automatically rotate proxies in sessions
Spiders
Use sessions in spider crawls