cfcrawler
Cloudflare scraper and cralwer written in Async, In-place library for HTTPX. Crawl website that has cloudflare enabled, easier than ever!
This library is a HTTP client designed to crawl websites protected by Cloudflare, even when their bot detection system is active. If you're already using httpx, you can switch to this library easily since it's a drop-in replacement. Just change the import, and you're good to go.
- Github repository: https://github.com/ManiMozaffar/cfcrawler/
Installation
To install base package, you can do:
pip install cfcrawler
To install fake-useragent backend support to rotate user agents, you can do:
pip install cfcrawler[ua]
Getting started
To use library, simply replace your aiohttp client with ours! It's completely in-place :)
from cfcrawler import AsyncClient
async def get(url):
client = AsyncClient()
await client.get(url)
By default, we're using one random user agent, which is still undetected in tests.
To rotate user agent, you need to explicitly call rotate_useragent method.
By default, we have a pool of few user agents, But you can also install extra optional dependencies to have a big pool of user agents to rotate between them.
from cfcrawler import AsyncClient
client = AsyncClient(use_fake_useragent_library=True) # one random user agent is selected
# do something
client.rotate_useragent() # user agent is rotated
# do something
client.rotate_useragent() # user agent is rotated again
# do something
You can also specify which browser you want to use
from cfcrawler.types import Browser
from cfcrawler import AsyncClient
AsyncClient(browser=Browser.CHROME)
If you wish to have your own user agent pool, you can pass the factory callable to the client
from cfcrawler import AsyncClient
def my_useragent_factory():
return "My User Agent" # your implementation
client = AsyncClient(user_agent_factory=my_useragent_factory)
You can also use asyncer to syncify the implementation
from cfcrawler import AsyncClient
from asyncer import syncify
def get(url):
client = AsyncClient()
syncify(client.get)(url)
How this library works
The Problem
Websites using Cloudflare often rely on a bot detection mechanism that works by checking your TLS Fingerprint. So, what's a TLS Fingerprint? When you connect to a website that uses HTTPS, the first thing that happens is the exchange of a "Client Hello" message. This message tells the server some basic info about your client, like the TLS version you support and a list of cipher suites, and etc.
What's a Cipher Suite?
A cipher suite is a set of cryptographic algorithms that the client and server use to establish a secure connection. Each browser or client has its own specific list of cipher suites, and their order is unique. For instance, Chrome has its own list, Firefox has another, and Python's requests library has a completely different one.
The Detection
Cloudflare figures out if you're not a real browser by comparing your TLS Fingerprint—which is a combination of the TLS version and cipher suite order—with your user-agent. If there's a mismatch, like if your user-agent says you're Chrome but your cipher suites suggest you're a Python script, Cloudflare knows you're not a browser and blocks the request.
How My Library Helps
This library handles that problem by aligning your TLS Fingerprint with your user-agent, making it harder for Cloudflare to detect that you're not a real browser. The best part? It's just 10 lines of code! In source code, checkout cfcrawler/tls.py to see how it works.
Coming Next
- CF JS Challenge solver
- Captcha solver integration (2Captcha and etc)
Metadata
Release files for cfcrawler 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cfcrawler-0.1.0.tar.gz | 9.4 kB | Without provenance | Details for cfcrawler-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cfcrawler-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for cfcrawler-0.1.0-py3-none-any.whl |
Total release size: 17.7 kB
Release files / cfcrawler-0.1.0.tar.gz
| Download URL | cfcrawler-0.1.0.tar.gz |
|---|---|
| Size | 9.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e063b4cee5acda3c13d44f2d34cdae9490e7a6c47195d6e87b029dd841a7d638
|
|
BLAKE2b-256 checksum How to use checksums |
abfb19c11ecb30a68fd774f927a1e9b1fad8449a33710fd3c0cc0628fdc7883d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.5
|
Release files / cfcrawler-0.1.0-py3-none-any.whl
| Download URL | cfcrawler-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d816838bbffb5ec94ef4918fc1ddadf89fcbb6de435f4c0794f0c362e269113f
|
|
BLAKE2b-256 checksum How to use checksums |
393ae230de878e17d19318646e966488c6b24984d43ae07e4c700375841c5ca5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.5
|
Security for cfcrawler 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.