Skip to main content

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

Project description

scrapy-selenium-cdp

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 middle more platform independent as no WebDriver is required.

🚧 Work in progress 🚧

Installation

pip install git+https://github.com/nyg/scrapy-seleniumbase-cdp

Configuration

  1. Provide keyword arguments for Driver in dict. For example:

    SELENIUMBASE_DRIVER_KWARGS = {
    "browser": "chrome",
    "uc": True,
    "headless": True,
    "block_images": True,
    }
    
  2. Add the SeleniumBaseMiddleware to the downloader middlewares:

    DOWNLOADER_MIDDLEWARES = {
        'scrapy_seleniumbase.SeleniumBaseMiddleware': 800
    }
    

Usage

Use the scrapy_seleniumbase.SeleniumBaseRequest instead of the scrapy built-in Request like below:

from scrapy_seleniumbase import SeleniumBaseRequest

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

The request will be handled by seleniumbase, and the request will have an additional meta key, named driver containing the seleniumbase driver with the request processed.

def parse_result(self, response):
    print(response.request.meta['driver'].title)

For more information about the available driver methods and attributes, refer to the selenium python documentation (all vanilla selenium driver methods are available) and seleniumbase documentation (look for "driver" specific methods, located at the end of the page).

The selector response attribute work as usual (but contains the html processed by the selenium driver).

def parse_result(self, response):
    print(response.selector.xpath('//title/@text'))

Additional arguments

The scrapy_selenium.SeleniumBaseRequest accept 5 additional arguments:

wait_time / wait_until

When used, webdriver will perform an explicit wait before returning the response to the spider.

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

yield SeleniumBaseRequest(
    url=url,
    callback=self.parse_result,
    wait_time=10,
    wait_until=EC.element_to_be_clickable((By.ID, 'someid'))
)

screenshot

When used, webdriver will take a screenshot of the page and the binary data of the .png captured will be added to the response meta:

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'])

script

When used, webdriver will execute custom JavaScript code.

yield SeleniumBaseRequest(
    url=url,
    callback=self.parse_result,
    script='window.scrollTo(0, document.body.scrollHeight);',
)

driver_methods

When used, seleniumbase webdriver will execute methods, provided as strings in a list, before returning page's html.

def start_requests(self):
    for url in self.start_urls:
        yield SeleniumRequest(
            url=url,
            driver_methods=['''.find_element("xpath","some_xpath").click()'''])

)

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-0.0.1.tar.gz (5.0 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-0.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: scrapy_seleniumbase_cdp-0.0.1.tar.gz
  • Upload date:
  • Size: 5.0 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-0.0.1.tar.gz
Algorithm Hash digest
SHA256 42469afb8dbaa3b9fad2b0ff7349a3d187675746914555caf1466a0694ae7f4d
MD5 3f07b5a9064aa9b4a3ede577f6500c07
BLAKE2b-256 d0941b8738463a74a071c06fc386887d0c2df7581d73caee7cc70370151e1653

See more details on using hashes here.

Provenance

The following attestation bundles were made for scrapy_seleniumbase_cdp-0.0.1.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-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for scrapy_seleniumbase_cdp-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ddd5a604859b83a6c87776be83b42dc21e9f5beb85314bbe25beeb62441fb620
MD5 d6093508525fcb62be713da9b7fa569f
BLAKE2b-256 f8d603afaa4d1a45f5c48ed445546567c02248ffe0199dc9132fe722bbadd7ec

See more details on using hashes here.

Provenance

The following attestation bundles were made for scrapy_seleniumbase_cdp-0.0.1-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