Manage Browser Contexts
mainYou can define multiple browser contexts at startup using the PLAYWRIGHT_CONTEXTS setting.
- Default Context: If no context is specified, requests use the
defaultcontext. - Choosing a Context: Use the
playwright_contextmeta key in yourscrapy.Requestto select a pre-defined context. - Creating New Contexts: If a named context does not exist, it will be created on the fly. You can pass configuration for this new context using the
playwright_context_kwargsmeta key. - Limiting Contexts: Use
PLAYWRIGHT_MAX_CONTEXTSto limit concurrent contexts and prevent resource exhaustion.
# Select a pre-defined context
yield scrapy.Request(
url="https://example.org",
meta={"playwright": True, "playwright_context": "first"},
)
# Create a new context with specific arguments
yield scrapy.Request(
url="https://example.org",
meta={
"playwright": True,
"playwright_context": "new",
"playwright_context_kwargs": {
"java_script_enabled": False,
"ignore_https_errors": True,
},
},
)