Skip to main content

Scrapy downloader middleware that uses SeleniumBase's pure CDP mode to make requests.

Project description

scrapy-seleniumbase-cdp

PyPI Python Versions License Downloads

Scrapy downloader middleware that uses SeleniumBase's pure CDP mode to make requests, allowing to bypass most anti-bot protections (e.g. CloudFlare).

Using Selenium's pure CDP mode also makes the middleware more platform independent as no WebDriver is required.

Installation

pip install scrapy-seleniumbase-cdp

Configuration

  1. Add the SeleniumBaseAsyncCDPMiddleware to the downloader middlewares:

    DOWNLOADER_MIDDLEWARES = {
        'scrapy_seleniumbase_cdp.SeleniumBaseAsyncCDPMiddleware': 800
    }
    
  2. If needed, configuration can be provided to the SeleniumBase browser instance:

    SELENIUMBASE_BROWSER_OPTIONS = {
        # …
    }
    
  3. Optionally, configure the backoff delay (in seconds) applied when a 429 response is received. The default value is 60 seconds:

    SELENIUMBASE_BACKOFF_ON_429 = 60
    

Usage

To have SeleniumBase handle requests, use the scrapy_seleniumbase_cdp.SeleniumBaseRequest instead of Scrapy's built-in Request:

from scrapy_seleniumbase_cdp import SeleniumBaseRequest

async def start(self):
    yield SeleniumBaseRequest(url=url, callback=self.parse_result)

Additional arguments

The scrapy_seleniumbase_cdp.SeleniumBaseRequest accepts five additional arguments. They are executed in the order presented below:

wait_for / wait_timeout

When used, SeleniumBase will wait for the element with the given CSS selector to appear. The default timeout value is of 10 seconds but can be changed if needed. If the element is not found within the timeout, the request is skipped (Scrapy's IgnoreRequest is raised) and a full-page debug screenshot is saved using SeleniumBase's default path.

yield SeleniumBaseRequest(
    url=url,
    callback=self.parse_result,
    wait_for='h1.some-class',
    wait_timeout=5))

browser_callback

If needed, it is possible to provide a callback to interact with the browser instance and/or its tabs. The return value of the async callback is stored in response.meta['callback'].

async def start(self):
    async def maximize_window(browser: Browser):
        await browser.main_tab.maximize()

    yield SeleniumBaseRequest(, browser_callback=maximize_window)

script

When used, SeleniumBase will execute the provided JavaScript code.

yield SeleniumBaseRequest(
    # …
    script='window.scrollTo(0, document.body.scrollHeight)')

If the script returns a Promise, it is possible to await its result:

yield SeleniumBaseRequest(
    # …
    script={
        'await_promise': True,
        'script': '''
            document.getElementById('onetrust-accept-btn-handler').click()
            new Promise(resolve => setTimeout(resolve, 1000))
        '''
    })

The result of the JavaScript code is stored in response.meta['script'].

screenshot

When used, SeleniumBase will take a screenshot of the page and the binary data will be stored in response.meta['screenshot']:

yield SeleniumBaseRequest(url=url, callback=self.parse_result, screenshot=True)


def parse_result(self, response):
    # …
    with open('image.png', 'wb') as image_file:
        image_file.write(response.meta['screenshot'])

You can also specify additional configuration options:

yield SeleniumBaseRequest(, screenshot={'format': 'jpg', 'full_page': False})

Or provide a path to automatically save the screenshot (in this case, the image data is not stored in the response):

yield SeleniumBaseRequest(, screenshot={'path': 'output/image.png'})

Available configuration keys:

  • path: File path where screenshot will be saved. Use auto for SeleniumBase default path. Leave empty to return data in response meta.
  • format: Image format, defaults to png, jpg also available.
  • full_page: Capture full page or just viewport, defaults to True.

Error handling

The middleware checks the HTTP status code right after loading the page:

  • Non-2xx responses: wait_for, browser_callback, and script are skipped. A screenshot is still taken if configured. The response is returned with the real status code.
  • 429 (Too Many Requests): in addition to the above, the middleware sleeps for SELENIUMBASE_BACKOFF_ON_429 seconds (default 60) before returning. This gives the target server time to recover. Ensure 429 is in your RETRY_HTTP_CODES Scrapy setting for automatic retries.
  • wait_for timeout: if the expected element is not found within wait_timeout seconds, a full-page debug screenshot is saved using SeleniumBase's default path and IgnoreRequest is raised, causing Scrapy to skip the request.

License

This project is licensed under the MIT License. It is a fork of Quartz-Core/scrapy-seleniumbase which was originally released under the WTFPL.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scrapy_seleniumbase_cdp-1.0.2.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scrapy_seleniumbase_cdp-1.0.2-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file scrapy_seleniumbase_cdp-1.0.2.tar.gz.

File metadata

  • Download URL: scrapy_seleniumbase_cdp-1.0.2.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for scrapy_seleniumbase_cdp-1.0.2.tar.gz
Algorithm Hash digest
SHA256 f6f74e720fde8bca8170c22b61078acded81d5bfb59d03b02d37ba841bdae665
MD5 0c8f3de25616239f1fdb229e514b6eae
BLAKE2b-256 5db442ec973bacc7c99d2ed2e57d71f96d01e2826296d5b1f88160b4be6391a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for scrapy_seleniumbase_cdp-1.0.2.tar.gz:

Publisher: publish.yml on nyg/scrapy-seleniumbase-cdp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file scrapy_seleniumbase_cdp-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for scrapy_seleniumbase_cdp-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3e6146b2f62397277460e5a0eaaf6e87a99b01501483cae774637eaf93a3d6a8
MD5 91a9133e46e076291c63bdbc449b65cb
BLAKE2b-256 fa392edec6a9a1249819273b4ff31b9b50c9fbb901c5031e2987edd020a56efa

See more details on using hashes here.

Provenance

The following attestation bundles were made for scrapy_seleniumbase_cdp-1.0.2-py3-none-any.whl:

Publisher: publish.yml on nyg/scrapy-seleniumbase-cdp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page