cfcrawler 0.0.2__tar.gz → 0.1.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.
- cfcrawler-0.1.0/PKG-INFO +122 -0
- cfcrawler-0.1.0/README.md +109 -0
- {cfcrawler-0.0.2 → cfcrawler-0.1.0}/cfcrawler/cipher.py +1 -3
- cfcrawler-0.1.0/cfcrawler/client.py +127 -0
- cfcrawler-0.1.0/cfcrawler/py.typed +0 -0
- cfcrawler-0.1.0/cfcrawler/tls.py +75 -0
- {cfcrawler-0.0.2 → cfcrawler-0.1.0}/cfcrawler/types.py +3 -3
- cfcrawler-0.1.0/cfcrawler/user_agent.py +54 -0
- cfcrawler-0.1.0/cfcrawler.egg-info/PKG-INFO +122 -0
- cfcrawler-0.1.0/cfcrawler.egg-info/SOURCES.txt +18 -0
- cfcrawler-0.1.0/cfcrawler.egg-info/dependency_links.txt +1 -0
- cfcrawler-0.1.0/cfcrawler.egg-info/requires.txt +5 -0
- cfcrawler-0.1.0/cfcrawler.egg-info/top_level.txt +1 -0
- cfcrawler-0.1.0/pyproject.toml +33 -0
- cfcrawler-0.1.0/setup.cfg +4 -0
- cfcrawler-0.1.0/tests/test_ciphers.py +15 -0
- cfcrawler-0.1.0/tests/test_detect.py +30 -0
- cfcrawler-0.1.0/tests/test_rotation.py +28 -0
- cfcrawler-0.0.2/PKG-INFO +0 -83
- cfcrawler-0.0.2/README.md +0 -63
- cfcrawler-0.0.2/cfcrawler/client.py +0 -151
- cfcrawler-0.0.2/cfcrawler/transport.py +0 -33
- cfcrawler-0.0.2/pyproject.toml +0 -57
- {cfcrawler-0.0.2 → cfcrawler-0.1.0}/LICENSE +0 -0
- {cfcrawler-0.0.2 → cfcrawler-0.1.0}/cfcrawler/__init__.py +0 -0
cfcrawler-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: cfcrawler
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Cloudflare scraper and cralwer written in Async
|
|
5
|
+
Author-email: Mani Mozaffar <mani.mozaffar@gmail.com>
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: httpx>=0.20
|
|
10
|
+
Requires-Dist: typing-extensions>=4.12.2
|
|
11
|
+
Provides-Extra: ua
|
|
12
|
+
Requires-Dist: fake-useragent>=1.5.1; extra == "ua"
|
|
13
|
+
|
|
14
|
+
# cfcrawler
|
|
15
|
+
|
|
16
|
+
[](https://img.shields.io/github/v/release/ManiMozaffar/cfcrawler)
|
|
17
|
+
[](https://github.com/ManiMozaffar/cfcrawler/actions/workflows/main.yml?query=branch%3Amain)
|
|
18
|
+
[](https://codecov.io/gh/ManiMozaffar/cfcrawler)
|
|
19
|
+
[](https://img.shields.io/github/commit-activity/m/ManiMozaffar/cfcrawler)
|
|
20
|
+
[](https://img.shields.io/github/license/ManiMozaffar/cfcrawler)
|
|
21
|
+
|
|
22
|
+
Cloudflare scraper and cralwer written in Async, In-place library for HTTPX. Crawl website that has cloudflare enabled, easier than ever!
|
|
23
|
+
|
|
24
|
+
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.
|
|
25
|
+
|
|
26
|
+
- **Github repository**: <https://github.com/ManiMozaffar/cfcrawler/>
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
To install base package, you can do:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install cfcrawler
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
To install `fake-useragent` backend support to rotate user agents, you can do:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install cfcrawler[ua]
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Getting started
|
|
43
|
+
|
|
44
|
+
To use library, simply replace your aiohttp client with ours! It's completely in-place :)
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
from cfcrawler import AsyncClient
|
|
48
|
+
|
|
49
|
+
async def get(url):
|
|
50
|
+
client = AsyncClient()
|
|
51
|
+
await client.get(url)
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
By default, we're using one random user agent, which is still undetected in tests.
|
|
56
|
+
To rotate user agent, you need to explicitly call `rotate_useragent` method.
|
|
57
|
+
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.
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from cfcrawler import AsyncClient
|
|
61
|
+
|
|
62
|
+
client = AsyncClient(use_fake_useragent_library=True) # one random user agent is selected
|
|
63
|
+
# do something
|
|
64
|
+
client.rotate_useragent() # user agent is rotated
|
|
65
|
+
# do something
|
|
66
|
+
client.rotate_useragent() # user agent is rotated again
|
|
67
|
+
# do something
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
You can also specify which browser you want to use
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from cfcrawler.types import Browser
|
|
74
|
+
from cfcrawler import AsyncClient
|
|
75
|
+
|
|
76
|
+
AsyncClient(browser=Browser.CHROME)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
If you wish to have your own user agent pool, you can pass the factory callable to the client
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from cfcrawler import AsyncClient
|
|
83
|
+
|
|
84
|
+
def my_useragent_factory():
|
|
85
|
+
return "My User Agent" # your implementation
|
|
86
|
+
|
|
87
|
+
client = AsyncClient(user_agent_factory=my_useragent_factory)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
You can also use asyncer to syncify the implementation
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from cfcrawler import AsyncClient
|
|
94
|
+
from asyncer import syncify
|
|
95
|
+
|
|
96
|
+
def get(url):
|
|
97
|
+
client = AsyncClient()
|
|
98
|
+
syncify(client.get)(url)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## How this library works
|
|
102
|
+
|
|
103
|
+
### The Problem
|
|
104
|
+
|
|
105
|
+
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.
|
|
106
|
+
|
|
107
|
+
### What's a Cipher Suite?
|
|
108
|
+
|
|
109
|
+
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.
|
|
110
|
+
|
|
111
|
+
### The Detection
|
|
112
|
+
|
|
113
|
+
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.
|
|
114
|
+
|
|
115
|
+
### How My Library Helps
|
|
116
|
+
|
|
117
|
+
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.
|
|
118
|
+
|
|
119
|
+
## Coming Next
|
|
120
|
+
|
|
121
|
+
1. CF JS Challenge solver
|
|
122
|
+
2. Captcha solver integration (2Captcha and etc)
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# cfcrawler
|
|
2
|
+
|
|
3
|
+
[](https://img.shields.io/github/v/release/ManiMozaffar/cfcrawler)
|
|
4
|
+
[](https://github.com/ManiMozaffar/cfcrawler/actions/workflows/main.yml?query=branch%3Amain)
|
|
5
|
+
[](https://codecov.io/gh/ManiMozaffar/cfcrawler)
|
|
6
|
+
[](https://img.shields.io/github/commit-activity/m/ManiMozaffar/cfcrawler)
|
|
7
|
+
[](https://img.shields.io/github/license/ManiMozaffar/cfcrawler)
|
|
8
|
+
|
|
9
|
+
Cloudflare scraper and cralwer written in Async, In-place library for HTTPX. Crawl website that has cloudflare enabled, easier than ever!
|
|
10
|
+
|
|
11
|
+
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.
|
|
12
|
+
|
|
13
|
+
- **Github repository**: <https://github.com/ManiMozaffar/cfcrawler/>
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
To install base package, you can do:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install cfcrawler
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
To install `fake-useragent` backend support to rotate user agents, you can do:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install cfcrawler[ua]
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Getting started
|
|
30
|
+
|
|
31
|
+
To use library, simply replace your aiohttp client with ours! It's completely in-place :)
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from cfcrawler import AsyncClient
|
|
35
|
+
|
|
36
|
+
async def get(url):
|
|
37
|
+
client = AsyncClient()
|
|
38
|
+
await client.get(url)
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
By default, we're using one random user agent, which is still undetected in tests.
|
|
43
|
+
To rotate user agent, you need to explicitly call `rotate_useragent` method.
|
|
44
|
+
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.
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
from cfcrawler import AsyncClient
|
|
48
|
+
|
|
49
|
+
client = AsyncClient(use_fake_useragent_library=True) # one random user agent is selected
|
|
50
|
+
# do something
|
|
51
|
+
client.rotate_useragent() # user agent is rotated
|
|
52
|
+
# do something
|
|
53
|
+
client.rotate_useragent() # user agent is rotated again
|
|
54
|
+
# do something
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
You can also specify which browser you want to use
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from cfcrawler.types import Browser
|
|
61
|
+
from cfcrawler import AsyncClient
|
|
62
|
+
|
|
63
|
+
AsyncClient(browser=Browser.CHROME)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
If you wish to have your own user agent pool, you can pass the factory callable to the client
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from cfcrawler import AsyncClient
|
|
70
|
+
|
|
71
|
+
def my_useragent_factory():
|
|
72
|
+
return "My User Agent" # your implementation
|
|
73
|
+
|
|
74
|
+
client = AsyncClient(user_agent_factory=my_useragent_factory)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
You can also use asyncer to syncify the implementation
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from cfcrawler import AsyncClient
|
|
81
|
+
from asyncer import syncify
|
|
82
|
+
|
|
83
|
+
def get(url):
|
|
84
|
+
client = AsyncClient()
|
|
85
|
+
syncify(client.get)(url)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## How this library works
|
|
89
|
+
|
|
90
|
+
### The Problem
|
|
91
|
+
|
|
92
|
+
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.
|
|
93
|
+
|
|
94
|
+
### What's a Cipher Suite?
|
|
95
|
+
|
|
96
|
+
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.
|
|
97
|
+
|
|
98
|
+
### The Detection
|
|
99
|
+
|
|
100
|
+
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.
|
|
101
|
+
|
|
102
|
+
### How My Library Helps
|
|
103
|
+
|
|
104
|
+
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.
|
|
105
|
+
|
|
106
|
+
## Coming Next
|
|
107
|
+
|
|
108
|
+
1. CF JS Challenge solver
|
|
109
|
+
2. Captcha solver integration (2Captcha and etc)
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import random
|
|
2
|
+
import typing
|
|
3
|
+
from functools import lru_cache
|
|
4
|
+
|
|
5
|
+
from httpx import AsyncHTTPTransport, _types
|
|
6
|
+
from httpx._client import AsyncClient as _AsyncClient
|
|
7
|
+
from httpx._config import (
|
|
8
|
+
DEFAULT_LIMITS,
|
|
9
|
+
DEFAULT_MAX_REDIRECTS,
|
|
10
|
+
DEFAULT_TIMEOUT_CONFIG,
|
|
11
|
+
Limits,
|
|
12
|
+
)
|
|
13
|
+
from httpx._transports.base import AsyncBaseTransport
|
|
14
|
+
from typing_extensions import assert_never
|
|
15
|
+
|
|
16
|
+
from cfcrawler.tls import mimic_tls_fingerprint_from_browser
|
|
17
|
+
from cfcrawler.types import Browser
|
|
18
|
+
from cfcrawler.user_agent import get_all_ua_for_specific_browser
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@lru_cache
|
|
22
|
+
def get_fake_ua_factory(browser: Browser):
|
|
23
|
+
try:
|
|
24
|
+
from fake_useragent import UserAgent
|
|
25
|
+
except ImportError:
|
|
26
|
+
raise ImportError(
|
|
27
|
+
"You need to install fake-useragent library to use this feature."
|
|
28
|
+
"Please run `pip install cfcrawler[ua]`"
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
if browser == Browser.CHROME:
|
|
32
|
+
browsers = ["chrome"]
|
|
33
|
+
elif browser == Browser.FIREFOX:
|
|
34
|
+
browsers = ["firefox"]
|
|
35
|
+
else:
|
|
36
|
+
assert_never(browser)
|
|
37
|
+
|
|
38
|
+
ua = UserAgent(browsers=browsers)
|
|
39
|
+
return ua
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class AsyncClient(_AsyncClient):
|
|
43
|
+
def __init__(
|
|
44
|
+
self,
|
|
45
|
+
*,
|
|
46
|
+
browser: typing.Optional[Browser] = None,
|
|
47
|
+
default_user_agent: typing.Optional[str] = None,
|
|
48
|
+
cipher_suite: typing.Optional[str] = None,
|
|
49
|
+
ecdh_curve: typing.Optional[str] = None,
|
|
50
|
+
user_agent_factory: typing.Optional[typing.Callable[[], str]] = None,
|
|
51
|
+
use_fake_useragent_library: bool = False,
|
|
52
|
+
transport: typing.Optional[AsyncHTTPTransport] = None,
|
|
53
|
+
auth: typing.Optional[_types.AuthTypes] = None,
|
|
54
|
+
params: typing.Optional[_types.QueryParamTypes] = None,
|
|
55
|
+
headers: typing.Optional[_types.HeaderTypes] = None,
|
|
56
|
+
cookies: typing.Optional[_types.CookieTypes] = None,
|
|
57
|
+
verify: _types.VerifyTypes = False,
|
|
58
|
+
cert: typing.Optional[_types.CertTypes] = None,
|
|
59
|
+
http1: bool = True,
|
|
60
|
+
http2: bool = False,
|
|
61
|
+
proxies: typing.Optional[_types.ProxiesTypes] = None,
|
|
62
|
+
mounts: typing.Optional[typing.Mapping[str, AsyncBaseTransport]] = None,
|
|
63
|
+
timeout: _types.TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
|
|
64
|
+
follow_redirects: bool = False,
|
|
65
|
+
limits: Limits = DEFAULT_LIMITS,
|
|
66
|
+
max_redirects: int = DEFAULT_MAX_REDIRECTS,
|
|
67
|
+
event_hooks: typing.Optional[
|
|
68
|
+
typing.Mapping[str, typing.List[typing.Callable[..., typing.Any]]]
|
|
69
|
+
] = None,
|
|
70
|
+
base_url: _types.URLTypes = "",
|
|
71
|
+
app: typing.Optional[typing.Callable[..., typing.Any]] = None,
|
|
72
|
+
trust_env: bool = True,
|
|
73
|
+
default_encoding: typing.Union[str, typing.Callable[[bytes], str]] = "utf-8",
|
|
74
|
+
):
|
|
75
|
+
self.browser: Browser = browser or random.choice(
|
|
76
|
+
[Browser.CHROME, Browser.FIREFOX]
|
|
77
|
+
)
|
|
78
|
+
self.user_agent_factory = user_agent_factory
|
|
79
|
+
self.use_fake_useragent_library = use_fake_useragent_library
|
|
80
|
+
self.default_user_agent = default_user_agent
|
|
81
|
+
self._custom_transport = transport or AsyncHTTPTransport()
|
|
82
|
+
self.ecdh_curve = ecdh_curve
|
|
83
|
+
self.cipher_suite = cipher_suite
|
|
84
|
+
|
|
85
|
+
super().__init__(
|
|
86
|
+
auth=auth,
|
|
87
|
+
params=params,
|
|
88
|
+
headers=headers or {},
|
|
89
|
+
cookies=cookies,
|
|
90
|
+
timeout=timeout,
|
|
91
|
+
follow_redirects=follow_redirects,
|
|
92
|
+
max_redirects=max_redirects,
|
|
93
|
+
event_hooks=event_hooks,
|
|
94
|
+
base_url=base_url,
|
|
95
|
+
verify=verify,
|
|
96
|
+
cert=cert,
|
|
97
|
+
http2=http2,
|
|
98
|
+
transport=self._custom_transport,
|
|
99
|
+
app=app,
|
|
100
|
+
http1=http1,
|
|
101
|
+
proxies=proxies,
|
|
102
|
+
limits=limits,
|
|
103
|
+
trust_env=trust_env,
|
|
104
|
+
default_encoding=default_encoding,
|
|
105
|
+
mounts=mounts,
|
|
106
|
+
)
|
|
107
|
+
self.rotate_useragent()
|
|
108
|
+
|
|
109
|
+
def rotate_useragent(self):
|
|
110
|
+
self.headers.update({"User-Agent": self.get_random_user_agent()})
|
|
111
|
+
mimic_tls_fingerprint_from_browser(
|
|
112
|
+
pool=self._custom_transport._pool,
|
|
113
|
+
browser=self.browser,
|
|
114
|
+
ecdh_curve=self.ecdh_curve,
|
|
115
|
+
cipher_suite=self.cipher_suite,
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
def get_random_user_agent(self) -> str:
|
|
119
|
+
if self.default_user_agent:
|
|
120
|
+
return self.default_user_agent
|
|
121
|
+
elif self.user_agent_factory:
|
|
122
|
+
return self.user_agent_factory()
|
|
123
|
+
elif self.use_fake_useragent_library:
|
|
124
|
+
ua = get_fake_ua_factory(self.browser)
|
|
125
|
+
return typing.cast(str, ua.random)
|
|
126
|
+
else:
|
|
127
|
+
return random.choice(get_all_ua_for_specific_browser(self.browser))
|
|
File without changes
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import ssl
|
|
2
|
+
import typing
|
|
3
|
+
from functools import lru_cache
|
|
4
|
+
|
|
5
|
+
import typing_extensions as te
|
|
6
|
+
from httpcore import AsyncConnectionPool, AsyncHTTPProxy, AsyncSOCKSProxy
|
|
7
|
+
|
|
8
|
+
from cfcrawler.types import Browser
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def mimic_tls_fingerprint_from_browser(
|
|
12
|
+
pool: typing.Union[AsyncConnectionPool, AsyncHTTPProxy, AsyncSOCKSProxy],
|
|
13
|
+
browser: Browser,
|
|
14
|
+
ecdh_curve: typing.Optional[str] = None,
|
|
15
|
+
cipher_suite: typing.Optional[str] = None,
|
|
16
|
+
):
|
|
17
|
+
if pool._ssl_context is None:
|
|
18
|
+
pool._ssl_context = ssl.create_default_context()
|
|
19
|
+
|
|
20
|
+
ecdh_curve = ecdh_curve or "secp384r1"
|
|
21
|
+
cipher_suite = cipher_suite or get_cipher_suite(browser)
|
|
22
|
+
pool._ssl_context.set_ecdh_curve(ecdh_curve)
|
|
23
|
+
pool._ssl_context.set_ciphers(cipher_suite)
|
|
24
|
+
pool._ssl_context.check_hostname = False
|
|
25
|
+
|
|
26
|
+
pool._ssl_context.minimum_version = ssl.TLSVersion.TLSv1_2
|
|
27
|
+
pool._ssl_context.maximum_version = ssl.TLSVersion.TLSv1_3
|
|
28
|
+
pool._ssl_context = pool._ssl_context
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@lru_cache
|
|
32
|
+
def get_cipher_suite(browser: Browser) -> str:
|
|
33
|
+
if browser == Browser.CHROME:
|
|
34
|
+
cipher_suites = [
|
|
35
|
+
"TLS_AES_128_GCM_SHA256",
|
|
36
|
+
"TLS_CHACHA20_POLY1305_SHA256",
|
|
37
|
+
"TLS_AES_256_GCM_SHA384",
|
|
38
|
+
"ECDHE-ECDSA-AES128-GCM-SHA256",
|
|
39
|
+
"ECDHE-RSA-AES128-GCM-SHA256",
|
|
40
|
+
"ECDHE-ECDSA-CHACHA20-POLY1305",
|
|
41
|
+
"TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
|
|
42
|
+
"ECDHE-RSA-CHACHA20-POLY1305",
|
|
43
|
+
"ECDHE-RSA-AES256-GCM-SHA384",
|
|
44
|
+
"ECDHE-ECDSA-AES256-SHA",
|
|
45
|
+
"ECDHE-ECDSA-AES128-SHA",
|
|
46
|
+
"ECDHE-RSA-AES128-SHA",
|
|
47
|
+
"ECDHE-RSA-AES256-SHA",
|
|
48
|
+
"AES128-GCM-SHA256",
|
|
49
|
+
"AES256-GCM-SHA384",
|
|
50
|
+
"AES128-SHA",
|
|
51
|
+
"AES256-SHA",
|
|
52
|
+
]
|
|
53
|
+
elif browser == Browser.FIREFOX:
|
|
54
|
+
cipher_suites = [
|
|
55
|
+
"TLS_AES_128_GCM_SHA256",
|
|
56
|
+
"TLS_CHACHA20_POLY1305_SHA256",
|
|
57
|
+
"TLS_AES_256_GCM_SHA384",
|
|
58
|
+
"ECDHE-ECDSA-AES128-GCM-SHA256",
|
|
59
|
+
"ECDHE-RSA-AES128-GCM-SHA256",
|
|
60
|
+
"ECDHE-ECDSA-CHACHA20-POLY1305",
|
|
61
|
+
"ECDHE-RSA-CHACHA20-POLY1305",
|
|
62
|
+
"ECDHE-ECDSA-AES256-GCM-SHA384",
|
|
63
|
+
"ECDHE-RSA-AES256-GCM-SHA384",
|
|
64
|
+
"ECDHE-ECDSA-AES256-SHA",
|
|
65
|
+
"ECDHE-ECDSA-AES128-SHA",
|
|
66
|
+
"ECDHE-RSA-AES128-SHA",
|
|
67
|
+
"ECDHE-RSA-AES256-SHA",
|
|
68
|
+
"AES128-GCM-SHA256",
|
|
69
|
+
"AES256-GCM-SHA384",
|
|
70
|
+
"AES128-SHA",
|
|
71
|
+
"AES256-SHA",
|
|
72
|
+
]
|
|
73
|
+
else:
|
|
74
|
+
te.assert_never(browser)
|
|
75
|
+
return ":".join(cipher_suites)
|
|
@@ -18,8 +18,8 @@ class StrEnum(str, Enum):
|
|
|
18
18
|
raise TypeError("%r is not a string" % (values[0],))
|
|
19
19
|
if len(values) >= 2:
|
|
20
20
|
# check that encoding argument is a string
|
|
21
|
-
if not isinstance(values[1], str):
|
|
22
|
-
raise TypeError("encoding must be a string, not %r" % (values[1],))
|
|
21
|
+
if not isinstance(values[1], str): # type: ignore
|
|
22
|
+
raise TypeError("encoding must be a string, not %r" % (values[1],)) # type: ignore
|
|
23
23
|
if len(values) == 3:
|
|
24
24
|
# check that errors argument is a string
|
|
25
25
|
if not isinstance(values[2], str):
|
|
@@ -29,7 +29,7 @@ class StrEnum(str, Enum):
|
|
|
29
29
|
member._value_ = value
|
|
30
30
|
return member
|
|
31
31
|
|
|
32
|
-
def _generate_next_value_(name, start, count, last_values):
|
|
32
|
+
def _generate_next_value_(name, start, count, last_values): # type: ignore
|
|
33
33
|
"""
|
|
34
34
|
Return the lower-cased version of the member name.
|
|
35
35
|
"""
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from functools import lru_cache
|
|
2
|
+
|
|
3
|
+
from typing_extensions import assert_never
|
|
4
|
+
|
|
5
|
+
from cfcrawler.types import Browser
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@lru_cache
|
|
9
|
+
def get_operating_systems() -> list[str]:
|
|
10
|
+
operating_systems = [
|
|
11
|
+
"Windows NT 10.0; Win64; x64",
|
|
12
|
+
"Windows NT 10.0; WOW64",
|
|
13
|
+
"Windows NT 6.1; Win64; x64",
|
|
14
|
+
"Macintosh; Intel Mac OS X 12.4",
|
|
15
|
+
"Macintosh; Intel Mac OS X 10.15",
|
|
16
|
+
"X11; Ubuntu; Linux x86_64",
|
|
17
|
+
"X11; Fedora; Linux x86_64",
|
|
18
|
+
]
|
|
19
|
+
return operating_systems
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def get_all_chrome_ua() -> list[str]:
|
|
23
|
+
operating_systems = get_operating_systems()
|
|
24
|
+
chrome_versions = [
|
|
25
|
+
"115.0.5790.102",
|
|
26
|
+
"114.0.5735.198",
|
|
27
|
+
"113.0.5672.126",
|
|
28
|
+
"112.0.5615.137",
|
|
29
|
+
"111.0.5563.110",
|
|
30
|
+
]
|
|
31
|
+
return [
|
|
32
|
+
f"Mozilla/5.0 ({os}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{version} Safari/537.36"
|
|
33
|
+
for os in operating_systems
|
|
34
|
+
for version in chrome_versions
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def get_all_fireox_ua() -> list[str]:
|
|
39
|
+
operating_systems = get_operating_systems()
|
|
40
|
+
firefox_versions = ["115.0", "114.0", "113.0", "112.0", "111.0", "110.0", "109.0"]
|
|
41
|
+
return [
|
|
42
|
+
f"Mozilla/5.0 ({os}) Gecko/20100101 Firefox/{version}"
|
|
43
|
+
for os in operating_systems
|
|
44
|
+
for version in firefox_versions
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def get_all_ua_for_specific_browser(browser: Browser) -> list[str]:
|
|
49
|
+
if browser == Browser.CHROME:
|
|
50
|
+
return get_all_chrome_ua()
|
|
51
|
+
elif browser == Browser.FIREFOX:
|
|
52
|
+
return get_all_fireox_ua()
|
|
53
|
+
else:
|
|
54
|
+
assert_never(browser)
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: cfcrawler
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Cloudflare scraper and cralwer written in Async
|
|
5
|
+
Author-email: Mani Mozaffar <mani.mozaffar@gmail.com>
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: httpx>=0.20
|
|
10
|
+
Requires-Dist: typing-extensions>=4.12.2
|
|
11
|
+
Provides-Extra: ua
|
|
12
|
+
Requires-Dist: fake-useragent>=1.5.1; extra == "ua"
|
|
13
|
+
|
|
14
|
+
# cfcrawler
|
|
15
|
+
|
|
16
|
+
[](https://img.shields.io/github/v/release/ManiMozaffar/cfcrawler)
|
|
17
|
+
[](https://github.com/ManiMozaffar/cfcrawler/actions/workflows/main.yml?query=branch%3Amain)
|
|
18
|
+
[](https://codecov.io/gh/ManiMozaffar/cfcrawler)
|
|
19
|
+
[](https://img.shields.io/github/commit-activity/m/ManiMozaffar/cfcrawler)
|
|
20
|
+
[](https://img.shields.io/github/license/ManiMozaffar/cfcrawler)
|
|
21
|
+
|
|
22
|
+
Cloudflare scraper and cralwer written in Async, In-place library for HTTPX. Crawl website that has cloudflare enabled, easier than ever!
|
|
23
|
+
|
|
24
|
+
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.
|
|
25
|
+
|
|
26
|
+
- **Github repository**: <https://github.com/ManiMozaffar/cfcrawler/>
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
To install base package, you can do:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install cfcrawler
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
To install `fake-useragent` backend support to rotate user agents, you can do:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install cfcrawler[ua]
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Getting started
|
|
43
|
+
|
|
44
|
+
To use library, simply replace your aiohttp client with ours! It's completely in-place :)
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
from cfcrawler import AsyncClient
|
|
48
|
+
|
|
49
|
+
async def get(url):
|
|
50
|
+
client = AsyncClient()
|
|
51
|
+
await client.get(url)
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
By default, we're using one random user agent, which is still undetected in tests.
|
|
56
|
+
To rotate user agent, you need to explicitly call `rotate_useragent` method.
|
|
57
|
+
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.
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from cfcrawler import AsyncClient
|
|
61
|
+
|
|
62
|
+
client = AsyncClient(use_fake_useragent_library=True) # one random user agent is selected
|
|
63
|
+
# do something
|
|
64
|
+
client.rotate_useragent() # user agent is rotated
|
|
65
|
+
# do something
|
|
66
|
+
client.rotate_useragent() # user agent is rotated again
|
|
67
|
+
# do something
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
You can also specify which browser you want to use
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from cfcrawler.types import Browser
|
|
74
|
+
from cfcrawler import AsyncClient
|
|
75
|
+
|
|
76
|
+
AsyncClient(browser=Browser.CHROME)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
If you wish to have your own user agent pool, you can pass the factory callable to the client
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from cfcrawler import AsyncClient
|
|
83
|
+
|
|
84
|
+
def my_useragent_factory():
|
|
85
|
+
return "My User Agent" # your implementation
|
|
86
|
+
|
|
87
|
+
client = AsyncClient(user_agent_factory=my_useragent_factory)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
You can also use asyncer to syncify the implementation
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from cfcrawler import AsyncClient
|
|
94
|
+
from asyncer import syncify
|
|
95
|
+
|
|
96
|
+
def get(url):
|
|
97
|
+
client = AsyncClient()
|
|
98
|
+
syncify(client.get)(url)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## How this library works
|
|
102
|
+
|
|
103
|
+
### The Problem
|
|
104
|
+
|
|
105
|
+
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.
|
|
106
|
+
|
|
107
|
+
### What's a Cipher Suite?
|
|
108
|
+
|
|
109
|
+
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.
|
|
110
|
+
|
|
111
|
+
### The Detection
|
|
112
|
+
|
|
113
|
+
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.
|
|
114
|
+
|
|
115
|
+
### How My Library Helps
|
|
116
|
+
|
|
117
|
+
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.
|
|
118
|
+
|
|
119
|
+
## Coming Next
|
|
120
|
+
|
|
121
|
+
1. CF JS Challenge solver
|
|
122
|
+
2. Captcha solver integration (2Captcha and etc)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
cfcrawler/__init__.py
|
|
5
|
+
cfcrawler/cipher.py
|
|
6
|
+
cfcrawler/client.py
|
|
7
|
+
cfcrawler/py.typed
|
|
8
|
+
cfcrawler/tls.py
|
|
9
|
+
cfcrawler/types.py
|
|
10
|
+
cfcrawler/user_agent.py
|
|
11
|
+
cfcrawler.egg-info/PKG-INFO
|
|
12
|
+
cfcrawler.egg-info/SOURCES.txt
|
|
13
|
+
cfcrawler.egg-info/dependency_links.txt
|
|
14
|
+
cfcrawler.egg-info/requires.txt
|
|
15
|
+
cfcrawler.egg-info/top_level.txt
|
|
16
|
+
tests/test_ciphers.py
|
|
17
|
+
tests/test_detect.py
|
|
18
|
+
tests/test_rotation.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
cfcrawler
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "cfcrawler"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Cloudflare scraper and cralwer written in Async"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.9"
|
|
7
|
+
authors = [{ name = "Mani Mozaffar", email = "mani.mozaffar@gmail.com" }]
|
|
8
|
+
dependencies = ["httpx>=0.20", "typing-extensions>=4.12.2"]
|
|
9
|
+
|
|
10
|
+
[tool.uv]
|
|
11
|
+
dev-dependencies = [
|
|
12
|
+
"pytest>=8.3.2",
|
|
13
|
+
"ruff>=0.6.3",
|
|
14
|
+
"pyright==1.1.353",
|
|
15
|
+
"fake-useragent>=1.5.1",
|
|
16
|
+
"pytest-asyncio<0.24",
|
|
17
|
+
"tox>=4.18.0",
|
|
18
|
+
"tox-gh-actions>=3.2.0",
|
|
19
|
+
"tox-uv>=1.11.2",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
[project.optional-dependencies]
|
|
24
|
+
ua = ["fake-useragent>=1.5.1"]
|
|
25
|
+
|
|
26
|
+
[tool.pyright]
|
|
27
|
+
typeCheckingMode = "standard"
|
|
28
|
+
|
|
29
|
+
[tool.pytest.ini_options]
|
|
30
|
+
addopts = "--tb=short --show-capture stdout"
|
|
31
|
+
|
|
32
|
+
[tool.setuptools.package-data]
|
|
33
|
+
"aioclock" = ["py.typed"]
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import ssl
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from cfcrawler.cipher import MAP_BROWSER_TO_CIPHER
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def test_valid_ciphers():
|
|
9
|
+
for browser, ciphers in MAP_BROWSER_TO_CIPHER.items():
|
|
10
|
+
cipher_suite = ":".join(ciphers)
|
|
11
|
+
try:
|
|
12
|
+
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
|
13
|
+
context.set_ciphers(cipher_suite)
|
|
14
|
+
except ssl.SSLError:
|
|
15
|
+
pytest.fail(f"Cipher {cipher_suite} is not supported for {browser}")
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from httpx import AsyncClient as DetectedClient
|
|
3
|
+
|
|
4
|
+
from cfcrawler.client import AsyncClient
|
|
5
|
+
from cfcrawler.types import Browser
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@pytest.mark.asyncio
|
|
9
|
+
async def test_patched_async_client():
|
|
10
|
+
client = AsyncClient(browser=Browser.CHROME)
|
|
11
|
+
resp = await client.get("https://www.futbin.com/")
|
|
12
|
+
assert resp.status_code == 200
|
|
13
|
+
assert "__cf_bm" in resp.cookies.keys()
|
|
14
|
+
|
|
15
|
+
client = AsyncClient(browser=Browser.FIREFOX)
|
|
16
|
+
resp = await client.get("https://www.futbin.com/")
|
|
17
|
+
assert resp.status_code == 200
|
|
18
|
+
assert "__cf_bm" in resp.cookies.keys()
|
|
19
|
+
|
|
20
|
+
client = AsyncClient(browser=Browser.FIREFOX, use_fake_useragent_library=True)
|
|
21
|
+
resp = await client.get("https://www.futbin.com/")
|
|
22
|
+
assert resp.status_code == 200
|
|
23
|
+
assert "__cf_bm" in resp.cookies.keys()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@pytest.mark.asyncio
|
|
27
|
+
async def test_of_assumption_tht_target_is_still_protected():
|
|
28
|
+
client = DetectedClient()
|
|
29
|
+
resp = await client.get("https://www.futbin.com/")
|
|
30
|
+
assert resp.status_code != 200
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from cfcrawler.client import AsyncClient
|
|
4
|
+
from cfcrawler.types import Browser
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.mark.asyncio
|
|
8
|
+
async def test_factory_works():
|
|
9
|
+
ua_pool = ["Mozilla/5.0", "Mozilla/6.0"]
|
|
10
|
+
client = AsyncClient(
|
|
11
|
+
browser=Browser.CHROME, user_agent_factory=lambda: ua_pool.pop()
|
|
12
|
+
)
|
|
13
|
+
assert client.headers["User-Agent"] == "Mozilla/6.0"
|
|
14
|
+
client.rotate_useragent()
|
|
15
|
+
assert client.headers["User-Agent"] == "Mozilla/5.0"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@pytest.mark.asyncio
|
|
19
|
+
async def test_rotate_works_with_fake_lib():
|
|
20
|
+
# This test is a bit flaky, but it's good enough for now.
|
|
21
|
+
client = AsyncClient(browser=Browser.CHROME, use_fake_useragent_library=True)
|
|
22
|
+
user_agents: set[str] = set()
|
|
23
|
+
for _ in range(10):
|
|
24
|
+
user_agent_one = client.headers["User-Agent"]
|
|
25
|
+
client.rotate_useragent()
|
|
26
|
+
user_agents.add(user_agent_one)
|
|
27
|
+
|
|
28
|
+
assert len(user_agents) > 1 # might have repeated user agents
|
cfcrawler-0.0.2/PKG-INFO
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: cfcrawler
|
|
3
|
-
Version: 0.0.2
|
|
4
|
-
Summary: Cloudflare scraper and cralwer written in Async
|
|
5
|
-
Home-page: https://github.com/ManiMozaffar/cfcrawler
|
|
6
|
-
Author: Mani Mozaffar
|
|
7
|
-
Author-email: fmani.mozaffar@gmail.com
|
|
8
|
-
Requires-Python: >=3.9,<4.0
|
|
9
|
-
Classifier: Programming Language :: Python :: 3
|
|
10
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
11
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
-
Requires-Dist: fake-useragent (>=1.4.0,<2.0.0)
|
|
15
|
-
Requires-Dist: httpx (>=0.25.2,<0.26.0)
|
|
16
|
-
Project-URL: Documentation, https://ManiMozaffar.github.io/cfcrawler/
|
|
17
|
-
Project-URL: Repository, https://github.com/ManiMozaffar/cfcrawler
|
|
18
|
-
Description-Content-Type: text/markdown
|
|
19
|
-
|
|
20
|
-
# cfcrawler
|
|
21
|
-
|
|
22
|
-
[](https://img.shields.io/github/v/release/ManiMozaffar/cfcrawler)
|
|
23
|
-
[](https://github.com/ManiMozaffar/cfcrawler/actions/workflows/main.yml?query=branch%3Amain)
|
|
24
|
-
[](https://codecov.io/gh/ManiMozaffar/cfcrawler)
|
|
25
|
-
[](https://img.shields.io/github/commit-activity/m/ManiMozaffar/cfcrawler)
|
|
26
|
-
[](https://img.shields.io/github/license/ManiMozaffar/cfcrawler)
|
|
27
|
-
|
|
28
|
-
Cloudflare scraper and cralwer written in Async, In-place library for HTTPX.
|
|
29
|
-
Crawl website that has cloudflare enabled, easier than ever!
|
|
30
|
-
|
|
31
|
-
- **Github repository**: <https://github.com/ManiMozaffar/cfcrawler/>
|
|
32
|
-
|
|
33
|
-
## Getting started
|
|
34
|
-
|
|
35
|
-
To use library, simply replace your aiohttp client with ours!
|
|
36
|
-
|
|
37
|
-
```python
|
|
38
|
-
from cfcrawler import AsyncClient
|
|
39
|
-
|
|
40
|
-
async def get(url):
|
|
41
|
-
client = AsyncClient()
|
|
42
|
-
await client.get(url)
|
|
43
|
-
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
You can also rotate user agents
|
|
47
|
-
|
|
48
|
-
```python
|
|
49
|
-
from cfcrawler import AsyncClient
|
|
50
|
-
|
|
51
|
-
client = AsyncClient()
|
|
52
|
-
client.rotate_useragent()
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
You can also specify which browser you want to use
|
|
56
|
-
```python
|
|
57
|
-
from cfcrawler.types import Browser
|
|
58
|
-
from cfcrawler import AsyncClient
|
|
59
|
-
|
|
60
|
-
AsyncClient(browser=Browser.CHROME)
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
You can also use asyncer to syncify the implementation
|
|
64
|
-
```python
|
|
65
|
-
from cfcrawler import AsyncClient
|
|
66
|
-
from asyncer import syncify
|
|
67
|
-
|
|
68
|
-
def get(url):
|
|
69
|
-
client = AsyncClient()
|
|
70
|
-
syncify(client.get)(url)
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
## Coming Next
|
|
74
|
-
|
|
75
|
-
1. CF JS Challenge solver
|
|
76
|
-
2. Captcha solver integration (2Captcha and etc)
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
## Contribution
|
|
80
|
-
|
|
81
|
-
I'll work on this library in few months, I don't have free time right now, but feel free to contribute.
|
|
82
|
-
I'll check and test the PRs myself!
|
|
83
|
-
|
cfcrawler-0.0.2/README.md
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
# cfcrawler
|
|
2
|
-
|
|
3
|
-
[](https://img.shields.io/github/v/release/ManiMozaffar/cfcrawler)
|
|
4
|
-
[](https://github.com/ManiMozaffar/cfcrawler/actions/workflows/main.yml?query=branch%3Amain)
|
|
5
|
-
[](https://codecov.io/gh/ManiMozaffar/cfcrawler)
|
|
6
|
-
[](https://img.shields.io/github/commit-activity/m/ManiMozaffar/cfcrawler)
|
|
7
|
-
[](https://img.shields.io/github/license/ManiMozaffar/cfcrawler)
|
|
8
|
-
|
|
9
|
-
Cloudflare scraper and cralwer written in Async, In-place library for HTTPX.
|
|
10
|
-
Crawl website that has cloudflare enabled, easier than ever!
|
|
11
|
-
|
|
12
|
-
- **Github repository**: <https://github.com/ManiMozaffar/cfcrawler/>
|
|
13
|
-
|
|
14
|
-
## Getting started
|
|
15
|
-
|
|
16
|
-
To use library, simply replace your aiohttp client with ours!
|
|
17
|
-
|
|
18
|
-
```python
|
|
19
|
-
from cfcrawler import AsyncClient
|
|
20
|
-
|
|
21
|
-
async def get(url):
|
|
22
|
-
client = AsyncClient()
|
|
23
|
-
await client.get(url)
|
|
24
|
-
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
You can also rotate user agents
|
|
28
|
-
|
|
29
|
-
```python
|
|
30
|
-
from cfcrawler import AsyncClient
|
|
31
|
-
|
|
32
|
-
client = AsyncClient()
|
|
33
|
-
client.rotate_useragent()
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
You can also specify which browser you want to use
|
|
37
|
-
```python
|
|
38
|
-
from cfcrawler.types import Browser
|
|
39
|
-
from cfcrawler import AsyncClient
|
|
40
|
-
|
|
41
|
-
AsyncClient(browser=Browser.CHROME)
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
You can also use asyncer to syncify the implementation
|
|
45
|
-
```python
|
|
46
|
-
from cfcrawler import AsyncClient
|
|
47
|
-
from asyncer import syncify
|
|
48
|
-
|
|
49
|
-
def get(url):
|
|
50
|
-
client = AsyncClient()
|
|
51
|
-
syncify(client.get)(url)
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
## Coming Next
|
|
55
|
-
|
|
56
|
-
1. CF JS Challenge solver
|
|
57
|
-
2. Captcha solver integration (2Captcha and etc)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
## Contribution
|
|
61
|
-
|
|
62
|
-
I'll work on this library in few months, I don't have free time right now, but feel free to contribute.
|
|
63
|
-
I'll check and test the PRs myself!
|
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import random
|
|
2
|
-
import typing
|
|
3
|
-
|
|
4
|
-
from fake_useragent import FakeUserAgent
|
|
5
|
-
from httpx import _types
|
|
6
|
-
from httpx._client import USE_CLIENT_DEFAULT, UseClientDefault
|
|
7
|
-
from httpx._client import AsyncClient as _AsyncClient
|
|
8
|
-
from httpx._config import (
|
|
9
|
-
DEFAULT_LIMITS,
|
|
10
|
-
DEFAULT_MAX_REDIRECTS,
|
|
11
|
-
DEFAULT_TIMEOUT_CONFIG,
|
|
12
|
-
Limits,
|
|
13
|
-
)
|
|
14
|
-
from httpx._models import Response
|
|
15
|
-
from httpx._transports.base import AsyncBaseTransport
|
|
16
|
-
|
|
17
|
-
from cfcrawler.transport import CfScrapeTransport
|
|
18
|
-
from cfcrawler.types import Browser
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
class AsyncClient(_AsyncClient):
|
|
22
|
-
def __init__(
|
|
23
|
-
self,
|
|
24
|
-
*,
|
|
25
|
-
browser: typing.Optional[Browser] = None,
|
|
26
|
-
default_user_agent: typing.Optional[str] = None,
|
|
27
|
-
auth: typing.Optional[_types.AuthTypes] = None,
|
|
28
|
-
cipher_suite: typing.Optional[str] = None,
|
|
29
|
-
ecdh_curve: typing.Optional[str] = None,
|
|
30
|
-
params: typing.Optional[_types.QueryParamTypes] = None,
|
|
31
|
-
headers: typing.Optional[_types.HeaderTypes] = None,
|
|
32
|
-
cookies: typing.Optional[_types.CookieTypes] = None,
|
|
33
|
-
verify: _types.VerifyTypes = False,
|
|
34
|
-
cert: typing.Optional[_types.CertTypes] = None,
|
|
35
|
-
http1: bool = True,
|
|
36
|
-
http2: bool = False,
|
|
37
|
-
proxies: typing.Optional[_types.ProxiesTypes] = None,
|
|
38
|
-
mounts: typing.Optional[typing.Mapping[str, AsyncBaseTransport]] = None,
|
|
39
|
-
timeout: _types.TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
|
|
40
|
-
follow_redirects: bool = False,
|
|
41
|
-
limits: Limits = DEFAULT_LIMITS,
|
|
42
|
-
max_redirects: int = DEFAULT_MAX_REDIRECTS,
|
|
43
|
-
event_hooks: typing.Optional[
|
|
44
|
-
typing.Mapping[str, typing.List[typing.Callable[..., typing.Any]]]
|
|
45
|
-
] = None,
|
|
46
|
-
base_url: _types.URLTypes = "",
|
|
47
|
-
transport: typing.Optional[AsyncBaseTransport] = None,
|
|
48
|
-
app: typing.Optional[typing.Callable[..., typing.Any]] = None,
|
|
49
|
-
trust_env: bool = True,
|
|
50
|
-
default_encoding: typing.Union[str, typing.Callable[[bytes], str]] = "utf-8",
|
|
51
|
-
):
|
|
52
|
-
self.browser: Browser = browser or random.choice(
|
|
53
|
-
[Browser.CHROME, Browser.FIREFOX]
|
|
54
|
-
)
|
|
55
|
-
self.ua = FakeUserAgent(self.browser.value)
|
|
56
|
-
transport = CfScrapeTransport(
|
|
57
|
-
browser=self.browser, cipher_suite=cipher_suite, ecdh_curve=ecdh_curve
|
|
58
|
-
)
|
|
59
|
-
self.default_user_agent = default_user_agent
|
|
60
|
-
|
|
61
|
-
super().__init__(
|
|
62
|
-
auth=auth,
|
|
63
|
-
params=params,
|
|
64
|
-
headers=headers,
|
|
65
|
-
cookies=cookies,
|
|
66
|
-
timeout=timeout,
|
|
67
|
-
follow_redirects=follow_redirects,
|
|
68
|
-
max_redirects=max_redirects,
|
|
69
|
-
event_hooks=event_hooks,
|
|
70
|
-
base_url=base_url,
|
|
71
|
-
verify=verify,
|
|
72
|
-
cert=cert,
|
|
73
|
-
http2=http2,
|
|
74
|
-
transport=transport,
|
|
75
|
-
app=app,
|
|
76
|
-
http1=http1,
|
|
77
|
-
proxies=proxies,
|
|
78
|
-
limits=limits,
|
|
79
|
-
trust_env=trust_env,
|
|
80
|
-
default_encoding=default_encoding,
|
|
81
|
-
mounts=mounts,
|
|
82
|
-
)
|
|
83
|
-
self.rotate_useragent()
|
|
84
|
-
|
|
85
|
-
async def request(
|
|
86
|
-
self,
|
|
87
|
-
method: str,
|
|
88
|
-
url: _types.URLTypes,
|
|
89
|
-
*,
|
|
90
|
-
content: typing.Optional[_types.RequestContent] = None,
|
|
91
|
-
data: typing.Optional[_types.RequestData] = None,
|
|
92
|
-
files: typing.Optional[_types.RequestFiles] = None,
|
|
93
|
-
json: typing.Optional[typing.Any] = None,
|
|
94
|
-
params: typing.Optional[_types.QueryParamTypes] = None,
|
|
95
|
-
headers: typing.Optional[_types.HeaderTypes] = None,
|
|
96
|
-
cookies: typing.Optional[_types.CookieTypes] = None,
|
|
97
|
-
auth: typing.Union[
|
|
98
|
-
_types.AuthTypes, UseClientDefault, None
|
|
99
|
-
] = USE_CLIENT_DEFAULT,
|
|
100
|
-
follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,
|
|
101
|
-
timeout: typing.Union[
|
|
102
|
-
_types.TimeoutTypes, UseClientDefault
|
|
103
|
-
] = USE_CLIENT_DEFAULT,
|
|
104
|
-
extensions: typing.Optional[_types.RequestExtensions] = None,
|
|
105
|
-
) -> Response:
|
|
106
|
-
"""
|
|
107
|
-
Build and send a request.
|
|
108
|
-
|
|
109
|
-
Equivalent to:
|
|
110
|
-
|
|
111
|
-
```python
|
|
112
|
-
request = client.build_request(...)
|
|
113
|
-
response = await client.send(request, ...)
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
See `AsyncClient.build_request()`, `AsyncClient.send()`
|
|
117
|
-
and [Merging of configuration][0] for how the various parameters
|
|
118
|
-
are merged with client-level configuration.
|
|
119
|
-
|
|
120
|
-
[0]: /advanced/#merging-of-configuration
|
|
121
|
-
"""
|
|
122
|
-
request = self.build_request(
|
|
123
|
-
method=method,
|
|
124
|
-
url=url,
|
|
125
|
-
content=content,
|
|
126
|
-
data=data,
|
|
127
|
-
files=files,
|
|
128
|
-
json=json,
|
|
129
|
-
params=params,
|
|
130
|
-
headers=headers,
|
|
131
|
-
cookies=cookies,
|
|
132
|
-
timeout=timeout,
|
|
133
|
-
extensions=extensions,
|
|
134
|
-
)
|
|
135
|
-
follow_redirects = True
|
|
136
|
-
response = await self.send(
|
|
137
|
-
request, auth=auth, follow_redirects=follow_redirects
|
|
138
|
-
)
|
|
139
|
-
return response
|
|
140
|
-
|
|
141
|
-
def inject_useragent(self, user_agent: str):
|
|
142
|
-
self.user_agent = user_agent
|
|
143
|
-
if self.default_user_agent:
|
|
144
|
-
self.user_agent = self.default_user_agent
|
|
145
|
-
else:
|
|
146
|
-
self.user_agent = user_agent
|
|
147
|
-
|
|
148
|
-
self.headers.update({"User-Agent": self.user_agent})
|
|
149
|
-
|
|
150
|
-
def rotate_useragent(self):
|
|
151
|
-
self.inject_useragent(self.ua.random)
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import ssl
|
|
2
|
-
import typing
|
|
3
|
-
|
|
4
|
-
from httpx import AsyncHTTPTransport
|
|
5
|
-
|
|
6
|
-
from cfcrawler.cipher import MAP_BROWSER_TO_CIPHER
|
|
7
|
-
from cfcrawler.types import Browser
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class CfScrapeTransport(AsyncHTTPTransport):
|
|
11
|
-
def __init__(
|
|
12
|
-
self,
|
|
13
|
-
browser: Browser,
|
|
14
|
-
ecdh_curve: typing.Optional[str] = None,
|
|
15
|
-
cipher_suite: typing.Optional[str] = None,
|
|
16
|
-
*args,
|
|
17
|
-
**kwargs,
|
|
18
|
-
) -> None:
|
|
19
|
-
super().__init__(*args, **kwargs)
|
|
20
|
-
|
|
21
|
-
ecdh_curve = ecdh_curve or "secp384r1"
|
|
22
|
-
map_browser_choice = MAP_BROWSER_TO_CIPHER[browser]
|
|
23
|
-
cipher_suite = cipher_suite or ":".join(map_browser_choice)
|
|
24
|
-
assert (
|
|
25
|
-
self._pool._ssl_context
|
|
26
|
-
), "SSL Context does not exists in the HTTPX connection pool"
|
|
27
|
-
self._pool._ssl_context.set_ecdh_curve(ecdh_curve)
|
|
28
|
-
self._pool._ssl_context.set_ciphers(cipher_suite)
|
|
29
|
-
self._pool._ssl_context.check_hostname = False
|
|
30
|
-
|
|
31
|
-
self._pool._ssl_context.minimum_version = ssl.TLSVersion.TLSv1_2
|
|
32
|
-
self._pool._ssl_context.maximum_version = ssl.TLSVersion.TLSv1_3
|
|
33
|
-
self._pool._ssl_context = self._pool._ssl_context
|
cfcrawler-0.0.2/pyproject.toml
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
[tool.poetry]
|
|
2
|
-
name = "cfcrawler"
|
|
3
|
-
version = "0.0.2"
|
|
4
|
-
description = "Cloudflare scraper and cralwer written in Async"
|
|
5
|
-
authors = ["Mani Mozaffar <fmani.mozaffar@gmail.com>"]
|
|
6
|
-
repository = "https://github.com/ManiMozaffar/cfcrawler"
|
|
7
|
-
documentation = "https://ManiMozaffar.github.io/cfcrawler/"
|
|
8
|
-
readme = "README.md"
|
|
9
|
-
packages = [{ include = "cfcrawler" }]
|
|
10
|
-
|
|
11
|
-
[tool.poetry.dependencies]
|
|
12
|
-
python = ">=3.9,<4.0"
|
|
13
|
-
fake-useragent = "^1.4.0"
|
|
14
|
-
httpx = "^0.25.2"
|
|
15
|
-
|
|
16
|
-
[tool.poetry.group.dev.dependencies]
|
|
17
|
-
pytest-asyncio = "^0.21.1"
|
|
18
|
-
pytest = "^7.2.0"
|
|
19
|
-
pytest-cov = "^4.0.0"
|
|
20
|
-
mypy = "^1.5.1"
|
|
21
|
-
pre-commit = "^3.4.0"
|
|
22
|
-
tox = "^4.11.1"
|
|
23
|
-
|
|
24
|
-
[tool.poetry.group.docs.dependencies]
|
|
25
|
-
mkdocs = "^1.4.2"
|
|
26
|
-
mkdocs-material = "^9.2.7"
|
|
27
|
-
mkdocstrings = { extras = ["python"], version = "^0.23.0" }
|
|
28
|
-
|
|
29
|
-
[build-system]
|
|
30
|
-
requires = ["poetry-core>=1.0.0"]
|
|
31
|
-
build-backend = "poetry.core.masonry.api"
|
|
32
|
-
|
|
33
|
-
[tool.black]
|
|
34
|
-
line-length = 120
|
|
35
|
-
target-version = ['py37']
|
|
36
|
-
preview = true
|
|
37
|
-
|
|
38
|
-
[tool.mypy]
|
|
39
|
-
files = ["cfcrawler"]
|
|
40
|
-
disallow_untyped_defs = "True"
|
|
41
|
-
disallow_any_unimported = "True"
|
|
42
|
-
no_implicit_optional = "True"
|
|
43
|
-
check_untyped_defs = "True"
|
|
44
|
-
warn_return_any = "True"
|
|
45
|
-
warn_unused_ignores = "True"
|
|
46
|
-
show_error_codes = "True"
|
|
47
|
-
|
|
48
|
-
[tool.pytest.ini_options]
|
|
49
|
-
testpaths = ["tests"]
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
[tool.coverage.report]
|
|
53
|
-
skip_empty = true
|
|
54
|
-
|
|
55
|
-
[tool.coverage.run]
|
|
56
|
-
branch = true
|
|
57
|
-
source = ["cfcrawler"]
|
|
File without changes
|
|
File without changes
|