Skip to main content

hamro-scrapy-playwright

Use scrapy-playwright with a custom stealth Chromium build.

Features

  • Automatic scrapy-playwright patching
  • Automatic browser download and management
  • Drop-in replacement for existing scrapy-playwright projects
  • Optional Cloudflare Turnstile auto-click integration

Install

pip install hamro-scrapy-playwright

Example

import scrapy
from scrapy import Request


class ExampleSpider(scrapy.Spider):
    name = "example"

    custom_settings = {
        "DOWNLOAD_HANDLERS": {
            "http": "hamro_scrapy_playwright.handler.HamroPlaywrightDownloadHandler",
            "https": "hamro_scrapy_playwright.handler.HamroPlaywrightDownloadHandler",
        },
        "TWISTED_REACTOR": "twisted.internet.asyncioreactor.AsyncioSelectorReactor",
        "PLAYWRIGHT_BROWSER_TYPE": "chromium",
        "PLAYWRIGHT_LAUNCH_OPTIONS": {
            "headless": False,
        },
        "PLAYWRIGHT_PROCESS_REQUEST_HEADERS": None,
    }

    async def start(self):
        yield Request(
            "https://www.example.com/",
            meta={
                "playwright": True,
                "playwright_include_page": True,
            },
            callback=self.parse,
        )

    async def parse(self, response):
        page = response.meta["playwright_page"]

        try:
            await page.wait_for_load_state("networkidle")

            await page.evaluate(
                """
                () => {
                    document.body.style.backgroundColor = "#225577";
                }
                """
            )

            await page.wait_for_timeout(4000)

            self.logger.info("Title: %s", await page.title())

        finally:
            await page.close()

Cloudflare Turnstile Auto-Click

To enable automatic Turnstile clicking, enable HAMRO_CF_AUTOCLICK and configure a persistent Playwright context.

Headful mode (recommended for development)

import tempfile

custom_settings = {
    "DOWNLOAD_HANDLERS": {
        "http": "hamro_scrapy_playwright.handler.HamroPlaywrightDownloadHandler",
        "https": "hamro_scrapy_playwright.handler.HamroPlaywrightDownloadHandler",
    },
    "TWISTED_REACTOR": "twisted.internet.asyncioreactor.AsyncioSelectorReactor",
    "PLAYWRIGHT_BROWSER_TYPE": "chromium",
    "PLAYWRIGHT_LAUNCH_OPTIONS": {
        "headless": False,
    },
    "PLAYWRIGHT_CONTEXTS": {
        "persistent": {
            "user_data_dir": tempfile.mkdtemp(),
        }
    },
    "HAMRO_CF_AUTOCLICK": True,
    "HTTPERROR_ALLOWED_CODES": [403, 429],
}

A visible browser window opens and the bundled cf-autoclick extension runs, so you can watch the Turnstile widget get clicked in real time.

Headless mode

If running headless, use Chromium's new headless mode (extensions do not load in the old headless mode):

"PLAYWRIGHT_LAUNCH_OPTIONS": {
    "headless": True,
    "args": ["--headless=new"],
}

Verify the extension is active

The extension registers a background service worker in the browser context. Check it from your spider:

for worker in page.context.service_workers:
    if "chrome-extension" in worker.url:
        self.logger.info("cf-autoclick extension is ACTIVE: %s", worker.url)

You should also see cf-autoclick extension enabled: <path> in the Scrapy logs at startup.

Why a persistent context?

The Cloudflare auto-click integration loads as a Chromium extension. Chromium extensions require a persistent browser profile (user_data_dir), so HAMRO_CF_AUTOCLICK only works when using a persistent Playwright context. PLAYWRIGHT_LAUNCH_OPTIONS are automatically merged into the persistent context by this package.

Metadata

Release files for hamro-scrapy-playwright 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hamro-scrapy-playwright 0.2.1
File Size Uploaded
hamro_scrapy_playwright-0.2.1.tar.gz 19.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hamro-scrapy-playwright 0.2.1
File Interpreter ABI Platform
hamro_scrapy_playwright-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 40.6 kB

Release files / hamro_scrapy_playwright-0.2.1.tar.gz

Download URL hamro_scrapy_playwright-0.2.1.tar.gz
Size 19.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d24d7b3f885a25d2c95923c4760e1ec8b3a21092cdfc1d0f00e81b17cf1f86c8
BLAKE2b-256 checksum
How to use checksums
aa2515229e0174ae2b3af2814923f6818382dd2f869da98f0f6cde11549a7f4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / hamro_scrapy_playwright-0.2.1-py3-none-any.whl

Download URL hamro_scrapy_playwright-0.2.1-py3-none-any.whl
Size 20.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
01257bdd4402bca95d5258bb1dfaff6adfced6a24e40c5f438944bc34cce858a
BLAKE2b-256 checksum
How to use checksums
eef5c5032c5af605e5ff75b5e671962346bbb5af4e87804d41eb5f7d39703ad9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page