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, see working example here. 🚧
Installation
pip install scrapy-seleniumbase-cdp
Configuration
-
Add the
SeleniumBaseAsyncCDPMiddlewareto the downloader middlewares:DOWNLOADER_MIDDLEWARES = { 'scrapy_seleniumbase_cdp.SeleniumBaseAsyncCDPMiddleware': 800 }
-
If needed, Driver configuration can be provided:
SELENIUMBASE_DRIVER_KWARGS = { # … }
Usage
Use the scrapy_seleniumbase_cdp.SeleniumBaseRequest instead of the scrapy
built-in Request like below:
from scrapy_seleniumbase_cdp 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file scrapy_seleniumbase_cdp-0.0.3.tar.gz.
File metadata
- Download URL: scrapy_seleniumbase_cdp-0.0.3.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8983ad7d5faef4698e60893fde67260608b49cc734330d076583360dac1a9935
|
|
| MD5 |
257262d13e32b5bd8ab0cfbc240231d7
|
|
| BLAKE2b-256 |
3ab4f4bbdd0429210ffabded0730ef2a83d4432d82425a68b37bd0fb730e48bd
|
Provenance
The following attestation bundles were made for scrapy_seleniumbase_cdp-0.0.3.tar.gz:
Publisher:
publish.yml on nyg/scrapy-seleniumbase-cdp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
scrapy_seleniumbase_cdp-0.0.3.tar.gz -
Subject digest:
8983ad7d5faef4698e60893fde67260608b49cc734330d076583360dac1a9935 - Sigstore transparency entry: 751163802
- Sigstore integration time:
-
Permalink:
nyg/scrapy-seleniumbase-cdp@9dcff67f04f6ec5c27d4e7ac95edb44f2a2132ac -
Branch / Tag:
refs/tags/v0.0.3 - Owner: https://github.com/nyg
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9dcff67f04f6ec5c27d4e7ac95edb44f2a2132ac -
Trigger Event:
release
-
Statement type:
File details
Details for the file scrapy_seleniumbase_cdp-0.0.3-py3-none-any.whl.
File metadata
- Download URL: scrapy_seleniumbase_cdp-0.0.3-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
365b2924e2935dd95811fb9d95bb3e3e8ae9068266b7b597764aa99077dd26a6
|
|
| MD5 |
33e90560458508841093e1c08d7bf46e
|
|
| BLAKE2b-256 |
a801356bc0fbe525a42af2a48f4b204aaada87e4e34f4b33b3278d80214c0095
|
Provenance
The following attestation bundles were made for scrapy_seleniumbase_cdp-0.0.3-py3-none-any.whl:
Publisher:
publish.yml on nyg/scrapy-seleniumbase-cdp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
scrapy_seleniumbase_cdp-0.0.3-py3-none-any.whl -
Subject digest:
365b2924e2935dd95811fb9d95bb3e3e8ae9068266b7b597764aa99077dd26a6 - Sigstore transparency entry: 751163873
- Sigstore integration time:
-
Permalink:
nyg/scrapy-seleniumbase-cdp@9dcff67f04f6ec5c27d4e7ac95edb44f2a2132ac -
Branch / Tag:
refs/tags/v0.0.3 - Owner: https://github.com/nyg
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9dcff67f04f6ec5c27d4e7ac95edb44f2a2132ac -
Trigger Event:
release
-
Statement type: