@@ -14,9 +14,11 @@ the cf_clearance, make sure you use the same IP and UA as when you got it.
14
14
15
15
## Warning
16
16
17
- Please use interface mode, You must add headless=False.
17
+ If you use chromium, Please use interface mode, You must add headless=False.
18
18
If you use it on linux or docker, use XVFB.
19
19
20
+ If you use firefox you don't need interface mode and XVFB.
21
+
20
22
Challenge are not always successful. Please try more and handle exceptions.
21
23
22
24
@@ -39,22 +41,39 @@ curl http://localhost:8000/challenge -H "Content-Type:application/json" -X POST
39
41
import requests
40
42
41
43
proxy = " socks5://localhost:7890"
42
- resp = requests.post(" http://localhost:8000/challenge" ,
43
- json = {" proxy" : {" server" : proxy}, " timeout" : 20 ,
44
- " url" : " https://nowsecure.nl" })
44
+ resp = requests.post(
45
+ " http://localhost:8000/challenge" ,
46
+ json = {
47
+ " proxy" : {" server" : proxy},
48
+ " timeout" : 20 ,
49
+ " url" : " https://nowsecure.nl" ,
50
+ " pure" : True ,
51
+ " browser" : 2 ,
52
+ " cookies" : [
53
+ {
54
+ " url" : " https://www.example.com" ,
55
+ " name" : " example-cookie" ,
56
+ " value" : " example-value" ,
57
+ }
58
+ ],
59
+ " headers" : {" example-ua" : " example-ua-value" },
60
+ " exec_js" : " () => {return navigator.userAgent}" ,
61
+ },
62
+ )
45
63
data = resp.json()
46
64
# In some cases, the cloudflare challenge will not be triggered,
47
65
# so when cf in the return parameter is true, it means that the challenge has been encountered.
48
66
if data.get(" success" ) and data.get(" cf" ):
49
67
ua = data.get(" user_agent" )
68
+ exec_js_resp = data.get(" exec_js_resp" )
50
69
cf_clearance_value = data.get(" cookies" ).get(" cf_clearance" )
51
70
# use cf_clearance, must be same IP and UA
52
71
headers = {" user-agent" : ua}
53
72
cookies = {" cf_clearance" : cf_clearance_value}
54
- res = requests.get(' https://nowsecure.nl ' , proxies = {
55
- " all" : proxy
56
- }, headers = headers, cookies = cookies )
57
- if ' <title>Just a moment...</title>' not in res.text:
73
+ res = requests.get(
74
+ " https://nowsecure.nl " , proxies = { " all" : proxy}, headers = headers, cookies = cookies
75
+ )
76
+ if " <title>Just a moment...</title>" not in res.text:
58
77
print (" cf challenge success" )
59
78
```
60
79
@@ -66,7 +85,7 @@ pip install cf-clearance
66
85
67
86
## Usage
68
87
69
- Please make sure it is the latest package.
88
+ Please make sure it is the latest package. See [ example ] ( https://github.com/vvanglro/cf-clearance/tree/main/example ) .
70
89
71
90
```
72
91
pip install --upgrade cf-clearance
76
95
pip install git+https://github.com/vvanglro/cf-clearance.git@main
77
96
```
78
97
79
- ### sync
80
-
81
- ``` python
82
- from playwright.sync_api import sync_playwright
83
- from cf_clearance import sync_cf_retry, sync_stealth
84
- import requests
85
-
86
- # not use cf_clearance, cf challenge is fail
87
- proxies = {
88
- " all" : " socks5://localhost:7890"
89
- }
90
- res = requests.get(' https://nowsecure.nl' , proxies = proxies)
91
- if ' <title>Just a moment...</title>' in res.text:
92
- print (" cf challenge fail" )
93
- # get cf_clearance
94
- with sync_playwright() as p:
95
- browser = p.chromium.launch(headless = False , proxy = {" server" : " socks5://localhost:7890" })
96
- page = browser.new_page()
97
- sync_stealth(page, pure = True )
98
- page.goto(' https://nowsecure.nl' )
99
- res, cf = sync_cf_retry(page)
100
- if cf:
101
- if res:
102
- cookies = page.context.cookies()
103
- for cookie in cookies:
104
- if cookie.get(' name' ) == ' cf_clearance' :
105
- cf_clearance_value = cookie.get(' value' )
106
- print (cf_clearance_value)
107
- ua = page.evaluate(' () => {return navigator.userAgent}' )
108
- print (ua)
109
- else :
110
- print (" cf challenge fail" )
111
- else :
112
- print (" No cloudflare challenges encountered" )
113
- browser.close()
114
- # use cf_clearance, must be same IP and UA
115
- headers = {" user-agent" : ua}
116
- cookies = {" cf_clearance" : cf_clearance_value}
117
- res = requests.get(' https://nowsecure.nl' , proxies = proxies, headers = headers, cookies = cookies)
118
- if ' <title>Just a moment...</title>' not in res.text:
119
- print (" cf challenge success" )
120
- ```
121
-
122
- ### async
123
-
124
- ``` python
125
- import asyncio
126
- from playwright.async_api import async_playwright
127
- from cf_clearance import async_cf_retry, async_stealth
128
- import requests
129
-
130
-
131
- async def main ():
132
- # not use cf_clearance, cf challenge is fail
133
- proxies = {
134
- " all" : " socks5://localhost:7890"
135
- }
136
- res = requests.get(' https://nowsecure.nl' , proxies = proxies)
137
- if ' <title>Just a moment...</title>' in res.text:
138
- print (" cf challenge fail" )
139
- # get cf_clearance
140
- async with async_playwright() as p:
141
- browser = await p.chromium.launch(headless = False , proxy = {" server" : " socks5://localhost:7890" })
142
- page = await browser.new_page()
143
- await async_stealth(page, pure = True )
144
- await page.goto(' https://nowsecure.nl' )
145
- res, cf = await async_cf_retry(page)
146
- if cf:
147
- if res:
148
- cookies = await page.context.cookies()
149
- for cookie in cookies:
150
- if cookie.get(' name' ) == ' cf_clearance' :
151
- cf_clearance_value = cookie.get(' value' )
152
- print (cf_clearance_value)
153
- ua = await page.evaluate(' () => {return navigator.userAgent}' )
154
- print (ua)
155
- else :
156
- print (" cf challenge fail" )
157
- else :
158
- print (" No cloudflare challenges encountered" )
159
- await browser.close()
160
- # use cf_clearance, must be same IP and UA
161
- headers = {" user-agent" : ua}
162
- cookies = {" cf_clearance" : cf_clearance_value}
163
- res = requests.get(' https://nowsecure.nl' , proxies = proxies, headers = headers, cookies = cookies)
164
- if ' <title>Just a moment...</title>' not in res.text:
165
- print (" cf challenge success" )
166
-
167
-
168
- asyncio.get_event_loop().run_until_complete(main())
98
+ ## Install Playwright Depends
99
+ ``` shell
100
+ playwright install chromium firefox
169
101
```
0 commit comments