Scrapy Playwright is a Scrapy Download Handler that integrates Playwright for Python, enabling Scrapy spiders to effectively scrape dynamic web pages that rely heavily on JavaScript rendering. It allows for browser automation within the Scrapy framework, facilitating interaction with complex web elements, while maintaining Scrapy's efficient crawling and scheduling model. The library is actively maintained with frequent releases, currently at version 0.0.46.
pip install scrapy-playwright playwrightVerified import paths — ran on the pinned version, not inferred.
To use Scrapy Playwright, you must configure your `settings.py` file to include the `ScrapyPlaywrightDownloadHandler` and set the `TWISTED_REACTOR` for asyncio compatibility. Then, within your spider, create `scrapy.Request` objects with `meta={'playwright': True}`. You can also define page interactions using `playwright_page_methods` with `PageMethod` objects to wait for elements or perform actions before parsing.
Upgrade Python to 3.10 or newer. Ensure your `requires_python` is set correctly.
Review and update any custom configurations or direct imports related to Scrapy's HTTP download handlers if they are dependent on `scrapy-playwright`'s internal path for this component.
Update custom `PLAYWRIGHT_PROCESS_REQUEST_HEADERS` functions to accept keyword arguments instead of positional ones.
Run `playwright install` in your terminal after installing the Python package to download the necessary browser binaries (Chromium, Firefox, WebKit).
Add `TWISTED_REACTOR = 'twisted.internet.asyncioreactor.AsyncioSelectorReactor'` to your `settings.py` file.
Manage concurrency using Scrapy's `CONCURRENT_REQUESTS` and `CONCURRENT_REQUESTS_PER_DOMAIN` settings. Additionally, `PLAYWRIGHT_MAX_PAGES_PER_CONTEXT` can limit Playwright's parallel page usage. Always use `headless=True` in `PLAYWRIGHT_LAUNCH_OPTIONS` unless debugging visually.
Avoid directly calling `Page.route` or `Page.unroute` on Playwright page objects obtained via `response.meta['playwright_page']` unless you have a deep understanding of the internal workings and are prepared for potential conflicts.