cfcrawler 0.0.2__tar.gz → 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. cfcrawler-1.0.0/.github/workflows/main.yml +64 -0
  2. cfcrawler-1.0.0/.github/workflows/on-release-main.yml +33 -0
  3. cfcrawler-1.0.0/.gitignore +169 -0
  4. cfcrawler-1.0.0/.pre-commit-config.yaml +5 -0
  5. cfcrawler-1.0.0/CONTRIBUTING.rst +151 -0
  6. cfcrawler-1.0.0/Makefile +15 -0
  7. cfcrawler-1.0.0/PKG-INFO +141 -0
  8. cfcrawler-1.0.0/README.md +124 -0
  9. cfcrawler-1.0.0/cfcrawler/client.py +175 -0
  10. cfcrawler-1.0.0/cfcrawler/py.typed +0 -0
  11. cfcrawler-1.0.0/cfcrawler/tls.py +71 -0
  12. cfcrawler-1.0.0/cfcrawler/transport.py +158 -0
  13. {cfcrawler-0.0.2 → cfcrawler-1.0.0}/cfcrawler/types.py +3 -3
  14. cfcrawler-1.0.0/cfcrawler/user_agent.py +106 -0
  15. cfcrawler-1.0.0/codecov.yaml +9 -0
  16. cfcrawler-1.0.0/docs/index.md +8 -0
  17. cfcrawler-1.0.0/docs/modules.md +1 -0
  18. cfcrawler-1.0.0/mkdocs.yml +54 -0
  19. cfcrawler-1.0.0/poetry.lock +1361 -0
  20. cfcrawler-1.0.0/pyproject.toml +48 -0
  21. cfcrawler-1.0.0/tests/__init__.py +0 -0
  22. cfcrawler-1.0.0/tests/conftest.py +11 -0
  23. cfcrawler-1.0.0/tests/test_ciphers.py +16 -0
  24. cfcrawler-1.0.0/tests/test_detect.py +84 -0
  25. cfcrawler-1.0.0/tests/test_rotation.py +57 -0
  26. cfcrawler-1.0.0/tests/test_transport.py +42 -0
  27. cfcrawler-1.0.0/tox.ini +46 -0
  28. cfcrawler-1.0.0/uv.lock +828 -0
  29. cfcrawler-0.0.2/PKG-INFO +0 -83
  30. cfcrawler-0.0.2/README.md +0 -63
  31. cfcrawler-0.0.2/cfcrawler/cipher.py +0 -44
  32. cfcrawler-0.0.2/cfcrawler/client.py +0 -151
  33. cfcrawler-0.0.2/cfcrawler/transport.py +0 -33
  34. cfcrawler-0.0.2/pyproject.toml +0 -57
  35. {cfcrawler-0.0.2 → cfcrawler-1.0.0}/LICENSE +0 -0
  36. {cfcrawler-0.0.2 → cfcrawler-1.0.0}/cfcrawler/__init__.py +0 -0
@@ -0,0 +1,64 @@
1
+ name: Main
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request:
8
+ types: [opened, synchronize, reopened, ready_for_review]
9
+
10
+ jobs:
11
+ run-quality-assurance:
12
+ runs-on: ubuntu-latest
13
+ strategy:
14
+ matrix:
15
+ python-version: ["3.14", "3.13", "3.12", "3.11", "3.10"]
16
+ fail-fast: false
17
+ steps:
18
+ - name: Check out
19
+ uses: actions/checkout@v3
20
+
21
+ - name: Set up python
22
+ uses: actions/setup-python@v4
23
+ with:
24
+ python-version: ${{ matrix.python-version }}
25
+
26
+ - uses: yezz123/setup-uv@v4
27
+
28
+ - name: Install dependencies
29
+ run: make install
30
+
31
+ - name: Run offline tests
32
+ run: uv run pytest -m "not live"
33
+
34
+ - name: Run checks
35
+ run: make check
36
+
37
+ live-tests:
38
+ # The `live-tests` environment requires a maintainer to approve each run and
39
+ # holds the residential proxy secret; fork PRs never receive secrets.
40
+ runs-on: ubuntu-latest
41
+ environment: live-tests
42
+ steps:
43
+ - name: Check out
44
+ uses: actions/checkout@v3
45
+
46
+ - name: Set up python
47
+ uses: actions/setup-python@v4
48
+ with:
49
+ python-version: "3.14"
50
+
51
+ - uses: yezz123/setup-uv@v4
52
+
53
+ - name: Install dependencies
54
+ run: make install
55
+
56
+ - name: Run live tests through the residential proxy
57
+ env:
58
+ CFCRAWLER_TEST_PROXY: ${{ secrets.CFCRAWLER_TEST_PROXY }}
59
+ run: |
60
+ if [ -z "$CFCRAWLER_TEST_PROXY" ]; then
61
+ echo "::error::CFCRAWLER_TEST_PROXY is not set in the live-tests environment"
62
+ exit 1
63
+ fi
64
+ uv run pytest -m live -rxX
@@ -0,0 +1,33 @@
1
+ name: release-main
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ jobs:
8
+ publish:
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - name: Check out
12
+ uses: actions/checkout@v4
13
+
14
+ - name: Set up uv
15
+ uses: astral-sh/setup-uv@v6
16
+
17
+ - name: Check version matches the release tag
18
+ run: |
19
+ tag="${GITHUB_REF_NAME#v}"
20
+ pkg="$(uv version --short)"
21
+ if [ "$tag" != "$pkg" ]; then
22
+ echo "::error::Release tag is $tag but pyproject.toml has $pkg. Bump the version before releasing."
23
+ exit 1
24
+ fi
25
+
26
+ # rye publish bundles an old twine that rejects current hatchling metadata (2.4+).
27
+ - name: Build
28
+ run: uv build
29
+
30
+ - name: Publish
31
+ run: uv publish --token "$PYPI_TOKEN"
32
+ env:
33
+ PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
@@ -0,0 +1,169 @@
1
+ docs/source
2
+
3
+ # From https://raw.githubusercontent.com/github/gitignore/main/Python.gitignore
4
+
5
+ # Byte-compiled / optimized / DLL files
6
+ __pycache__/
7
+ *.py[cod]
8
+ *$py.class
9
+
10
+ # C extensions
11
+ *.so
12
+
13
+ # Distribution / packaging
14
+ .python-version
15
+ .DS_Store
16
+ .Python
17
+ build/
18
+ develop-eggs/
19
+ dist/
20
+ downloads/
21
+ eggs/
22
+ .eggs/
23
+ lib/
24
+ lib64/
25
+ parts/
26
+ sdist/
27
+ var/
28
+ wheels/
29
+ share/python-wheels/
30
+ *.egg-info/
31
+ .installed.cfg
32
+ *.egg
33
+ MANIFEST
34
+
35
+ # PyInstaller
36
+ # Usually these files are written by a python script from a template
37
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
38
+ *.manifest
39
+ *.spec
40
+
41
+ # Installer logs
42
+ pip-log.txt
43
+ pip-delete-this-directory.txt
44
+
45
+ # Unit test / coverage reports
46
+ htmlcov/
47
+ .tox/
48
+ .nox/
49
+ .coverage
50
+ .coverage.*
51
+ .cache
52
+ nosetests.xml
53
+ coverage.xml
54
+ *.cover
55
+ *.py,cover
56
+ .hypothesis/
57
+ .pytest_cache/
58
+ cover/
59
+
60
+ # Translations
61
+ *.mo
62
+ *.pot
63
+
64
+ # Django stuff:
65
+ *.log
66
+ local_settings.py
67
+ db.sqlite3
68
+ db.sqlite3-journal
69
+
70
+ # Flask stuff:
71
+ instance/
72
+ .webassets-cache
73
+
74
+ # Scrapy stuff:
75
+ .scrapy
76
+
77
+ # Sphinx documentation
78
+ docs/_build/
79
+
80
+ # PyBuilder
81
+ .pybuilder/
82
+ target/
83
+
84
+ # Jupyter Notebook
85
+ .ipynb_checkpoints
86
+
87
+ # IPython
88
+ profile_default/
89
+ ipython_config.py
90
+
91
+ # pyenv
92
+ # For a library or package, you might want to ignore these files since the code is
93
+ # intended to run in multiple environments; otherwise, check them in:
94
+ # .python-version
95
+
96
+ # pipenv
97
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
98
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
99
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
100
+ # install all needed dependencies.
101
+ #Pipfile.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+
110
+ # pdm
111
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112
+ #pdm.lock
113
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114
+ # in version control.
115
+ # https://pdm.fming.dev/#use-with-ide
116
+ .pdm.toml
117
+
118
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
119
+ __pypackages__/
120
+
121
+ # Celery stuff
122
+ celerybeat-schedule
123
+ celerybeat.pid
124
+
125
+ # SageMath parsed files
126
+ *.sage.py
127
+
128
+ # Environments
129
+ .env
130
+ .venv
131
+ env/
132
+ venv/
133
+ ENV/
134
+ env.bak/
135
+ venv.bak/
136
+
137
+ # Spyder project settings
138
+ .spyderproject
139
+ .spyproject
140
+
141
+ # Rope project settings
142
+ .ropeproject
143
+
144
+ # mkdocs documentation
145
+ /site
146
+
147
+ # mypy
148
+ .mypy_cache/
149
+ .dmypy.json
150
+ dmypy.json
151
+
152
+ # Pyre type checker
153
+ .pyre/
154
+
155
+ # pytype static type analyzer
156
+ .pytype/
157
+
158
+ # Cython debug symbols
159
+ cython_debug/
160
+
161
+ # Vscode config files
162
+ .vscode/
163
+
164
+ # PyCharm
165
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
166
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
167
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
168
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
169
+ #.idea/
@@ -0,0 +1,5 @@
1
+ repos:
2
+ - repo: https://github.com/psf/black
3
+ rev: "22.8.0"
4
+ hooks:
5
+ - id: black
@@ -0,0 +1,151 @@
1
+ ============
2
+ Contributing
3
+ ============
4
+
5
+ Contributions are welcome, and they are greatly appreciated! Every little bit
6
+ helps, and credit will always be given.
7
+
8
+ You can contribute in many ways:
9
+
10
+ Types of Contributions
11
+ ----------------------
12
+
13
+ Report Bugs
14
+ ~~~~~~~~~~~
15
+
16
+ Report bugs at https://github.com/ManiMozaffar/cfcrawler/issues
17
+
18
+ If you are reporting a bug, please include:
19
+
20
+ * Your operating system name and version.
21
+ * Any details about your local setup that might be helpful in troubleshooting.
22
+ * Detailed steps to reproduce the bug.
23
+
24
+ Fix Bugs
25
+ ~~~~~~~~
26
+
27
+ Look through the GitHub issues for bugs. Anything tagged with "bug"
28
+ and "help wanted" is open to whoever wants to implement a fix for it.
29
+
30
+ Implement Features
31
+ ~~~~~~~~~~~~~~~~~~
32
+
33
+ Look through the GitHub issues for features. Anything tagged with "enhancement"
34
+ and "help wanted" is open to whoever wants to implement it.
35
+
36
+ Write Documentation
37
+ ~~~~~~~~~~~~~~~~~~~
38
+
39
+ Cookiecutter PyPackage could always use more documentation, whether as part of
40
+ the official docs, in docstrings, or even on the web in blog posts, articles,
41
+ and such.
42
+
43
+ Submit Feedback
44
+ ~~~~~~~~~~~~~~~
45
+
46
+ The best way to send feedback is to file an issue at
47
+ https://github.com/ManiMozaffar/cfcrawler/issues.
48
+
49
+ If you are proposing a new feature:
50
+
51
+ * Explain in detail how it would work.
52
+ * Keep the scope as narrow as possible, to make it easier to implement.
53
+ * Remember that this is a volunteer-driven project, and that contributions
54
+ are welcome :)
55
+
56
+ Get Started!
57
+ ------------
58
+
59
+ Ready to contribute? Here's how to set up `cfcrawler` for local
60
+ development. Please note this documentation assumes you already have
61
+ `poetry` and `Git` installed and ready to go.
62
+
63
+ | 1. Fork the `cfcrawler` repo on GitHub.
64
+
65
+ | 2. Clone your fork locally:
66
+
67
+ .. code-block:: bash
68
+
69
+ cd <directory_in_which_repo_should_be_created>
70
+ git clone git@github.com:YOUR_NAME/cfcrawler.git
71
+
72
+
73
+ | 3. Now we need to install the environment. Navigate into the directory
74
+
75
+ .. code-block:: bash
76
+
77
+ cd cfcrawler
78
+
79
+ If you are using ``pyenv``, select a version to use locally. (See installed versions with ``pyenv versions``)
80
+
81
+ .. code-block:: bash
82
+
83
+ pyenv local <x.y.z>
84
+
85
+ Then, install and activate the environment with:
86
+
87
+ .. code-block:: bash
88
+
89
+ poetry install
90
+ poetry shell
91
+
92
+ | 4. Install pre-commit to run linters/formatters at commit time:
93
+
94
+ .. code-block:: bash
95
+
96
+ poetry run pre-commit install
97
+
98
+ | 5. Create a branch for local development:
99
+
100
+ .. code-block:: bash
101
+
102
+ git checkout -b name-of-your-bugfix-or-feature
103
+
104
+ Now you can make your changes locally.
105
+
106
+
107
+ | 6. Don't forget to add test cases for your added functionality to the ``tests`` directory.
108
+
109
+ | 7. When you're done making changes, check that your changes pass the formatting tests.
110
+
111
+ .. code-block:: bash
112
+
113
+ make check
114
+
115
+ | 8. Now, validate that all unit tests are passing:
116
+
117
+ .. code-block:: bash
118
+
119
+ make test
120
+
121
+ | 9. Before raising a pull request you should also run tox. This will run the
122
+ tests across different versions of Python:
123
+
124
+ .. code-block:: bash
125
+
126
+ tox
127
+
128
+ This requires you to have multiple versions of python installed.
129
+ This step is also triggered in the CI/CD pipeline, so you could also choose to skip this
130
+ step locally.
131
+
132
+ | 10. Commit your changes and push your branch to GitHub:
133
+
134
+ .. code-block:: bash
135
+
136
+ git add .
137
+ git commit -m "Your detailed description of your changes."
138
+ git push origin name-of-your-bugfix-or-feature
139
+
140
+ | 11. Submit a pull request through the GitHub website.
141
+
142
+ Pull Request Guidelines
143
+ ---------------------------
144
+
145
+ Before you submit a pull request, check that it meets these guidelines:
146
+
147
+ 1. The pull request should include tests.
148
+
149
+ 2. If the pull request adds functionality, the docs should be updated. Put your
150
+ new functionality into a function with a docstring, and add the feature to
151
+ the list in README.rst.
@@ -0,0 +1,15 @@
1
+ .PHONY: install
2
+ install: ## Install the uv environment and install the pre-commit hooks
3
+ @echo "🚀 Creating virtual environment using uv"
4
+ @uv sync
5
+
6
+ .PHONY: test
7
+ test: ## Test the code with pytest
8
+ @echo "🚀 Testing code: Running pytest"
9
+ @uv run pytest
10
+
11
+ .PHONY: check
12
+ check: ## check the code with flake8
13
+ @echo "🚀 Checking code with ruff and pyright"
14
+ @uv run ruff check .
15
+ @uv run pyright
@@ -0,0 +1,141 @@
1
+ Metadata-Version: 2.5
2
+ Name: cfcrawler
3
+ Version: 1.0.0
4
+ Summary: Cloudflare scraper and cralwer written in Async
5
+ Project-URL: Homepage, https://github.com/ManiMozaffar/cfcrawler
6
+ Project-URL: Source, https://github.com/ManiMozaffar/cfcrawler
7
+ Author-email: Mani Mozaffar <mani.mozaffar@gmail.com>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Requires-Python: >=3.10
11
+ Requires-Dist: curl-cffi>=0.17
12
+ Requires-Dist: httpx[brotli,zstd]<0.28,>=0.27
13
+ Requires-Dist: typing-extensions>=4.12.2
14
+ Provides-Extra: ua
15
+ Requires-Dist: fake-useragent>=1.5.1; extra == 'ua'
16
+ Description-Content-Type: text/markdown
17
+
18
+ # cfcrawler
19
+
20
+ [![Release](https://img.shields.io/github/v/release/ManiMozaffar/cfcrawler)](https://img.shields.io/github/v/release/ManiMozaffar/cfcrawler)
21
+ [![Build status](https://img.shields.io/github/actions/workflow/status/ManiMozaffar/cfcrawler/main.yml?branch=main)](https://github.com/ManiMozaffar/cfcrawler/actions/workflows/main.yml?query=branch%3Amain)
22
+ [![codecov](https://codecov.io/gh/ManiMozaffar/cfcrawler/branch/main/graph/badge.svg)](https://codecov.io/gh/ManiMozaffar/cfcrawler)
23
+ [![Commit activity](https://img.shields.io/github/commit-activity/m/ManiMozaffar/cfcrawler)](https://img.shields.io/github/commit-activity/m/ManiMozaffar/cfcrawler)
24
+ [![License](https://img.shields.io/github/license/ManiMozaffar/cfcrawler)](https://img.shields.io/github/license/ManiMozaffar/cfcrawler)
25
+
26
+ Cloudflare scraper and cralwer written in Async, In-place library for HTTPX. Crawl website that has cloudflare enabled, easier than ever!
27
+
28
+ 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.
29
+
30
+ - **Github repository**: <https://github.com/ManiMozaffar/cfcrawler/>
31
+
32
+ ## Installation
33
+
34
+ To install base package, you can do:
35
+
36
+ ```bash
37
+ pip install cfcrawler
38
+ ```
39
+
40
+ To install `fake-useragent` backend support to rotate user agents, you can do:
41
+
42
+ ```bash
43
+ pip install "cfcrawler[ua]"
44
+ ```
45
+
46
+ ## Getting started
47
+
48
+ To use library, simply replace your aiohttp client with ours! It's completely in-place :)
49
+
50
+ ```python
51
+ from cfcrawler import AsyncClient
52
+
53
+ async def get(url):
54
+ client = AsyncClient()
55
+ await client.get(url)
56
+
57
+ ```
58
+
59
+ By default, we're using one random user agent, which is still undetected in tests.
60
+ To rotate user agent, you need to explicitly call `rotate_useragent` method.
61
+ 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.
62
+
63
+ ```python
64
+ from cfcrawler import AsyncClient
65
+
66
+ client = AsyncClient(use_fake_useragent_library=True) # one random user agent is selected
67
+ # do something
68
+ client.rotate_useragent() # user agent is rotated
69
+ # do something
70
+ client.rotate_useragent() # user agent is rotated again
71
+ # do something
72
+ ```
73
+
74
+ You can also specify which browser you want to use
75
+
76
+ ```python
77
+ from cfcrawler.types import Browser
78
+ from cfcrawler import AsyncClient
79
+
80
+ AsyncClient(browser=Browser.CHROME)
81
+ ```
82
+
83
+ If you wish to have your own user agent pool, you can pass the factory callable to the client
84
+
85
+ ```python
86
+ from cfcrawler import AsyncClient
87
+
88
+ def my_useragent_factory():
89
+ return "My User Agent" # your implementation
90
+
91
+ client = AsyncClient(user_agent_factory=my_useragent_factory)
92
+ ```
93
+
94
+ You can also use asyncer to syncify the implementation
95
+
96
+ ```python
97
+ from cfcrawler import AsyncClient
98
+ from asyncer import syncify
99
+
100
+ def get(url):
101
+ client = AsyncClient()
102
+ syncify(client.get)(url)
103
+ ```
104
+
105
+ ## How this library works
106
+
107
+ ### The Problem
108
+
109
+ 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.
110
+
111
+ ### What's a Cipher Suite?
112
+
113
+ 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.
114
+
115
+ ### The Detection
116
+
117
+ 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.
118
+
119
+ ### How My Library Helps
120
+
121
+ Matching only the cipher suites is no longer enough: Cloudflare and Akamai look at the whole Client Hello (supported groups, GREASE, extensions such as ALPS and certificate compression) and at HTTP/2 settings. Python's `ssl` module sits on OpenSSL, which cannot produce any of those browser-specific fields.
122
+
123
+ So cfcrawler sends requests through [curl_cffi](https://github.com/lexiforest/curl_cffi), whose bundled curl is built on BoringSSL (Chrome's TLS library) and reproduces real browser handshakes. It is plugged in as an httpx transport, so `AsyncClient` is still a regular `httpx.AsyncClient`: headers, cookies, redirects, auth, event hooks and `httpx.Response` all behave as before. cfcrawler also sends the browser's own headers (`sec-ch-ua`, `sec-fetch-*`, ...) in the browser's order, matched to the rotated user agent.
124
+
125
+ `http2=True` is the default, because a "browser" that only speaks HTTP/1.1 is itself a bot signal. If you pass your own `transport=`, cfcrawler falls back to the old OpenSSL cipher tuning on it.
126
+
127
+ Requires Python 3.10+.
128
+
129
+ ## Running the tests
130
+
131
+ ```bash
132
+ uv run pytest -m "not live" # offline
133
+ uv run pytest -m live -rxX # hits futbin (Cloudflare) and EA Pro Clubs (Akamai)
134
+ ```
135
+
136
+ Bot protection also scores IP reputation, and datacenter IPs (like CI runners) get challenged regardless of fingerprint. Live tests therefore read a rotating residential proxy from `CFCRAWLER_TEST_PROXY` (e.g. `http://login:password@host:port`), either from your shell or from a gitignored `.env` file. Each live test retries on up to 3 fresh exit IPs; if all are blocked it is reported as XFAIL instead of failing. On GitHub the proxy lives in the `live-tests` environment, whose runs need a maintainer's approval.
137
+
138
+ ## Coming Next
139
+
140
+ 1. CF JS Challenge solver
141
+ 2. Captcha solver integration (2Captcha and etc)