...
代码块 |
---|
import requests import time import re # 创建任务 def create_task(url, proxy): data = { # 填您自己的密钥 "clientKey": clientKey, "task": { "type": "CloudFlareTaskS2", # 指定chrome120内核 "userAgent":指定内核 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120126.0.0.0 Safari/537.36" "userAgent": "", "waitLoad": False, "websiteURL": url, "proxy": proxy } } url = "https://api.yescaptcha.com/createTask" # url = "http://localhost:8000/createTask" response = requests.post(url, json=data).json() return response # 获取结果 def get_task(task_id): url = "http://api.yescaptcha.com/getTaskResult" data = { # 填您自己的密钥 "clientKey": clientKey, "taskId": task_id } response = requests.post(url, json=data).json() return response # 完整的请求 def get_result(*args, **kwargs): uuid = create_task(*args, **kwargs) if not uuid or not uuid.get('taskId'): return uuid print("TaskID:", uuid) for i in range(30): time.sleep(3) result = get_task(uuid.get('taskId')) if result.get('status') == 'processing': continue elif result.get('status') == 'ready': return result else: raise Exception(result) if __name__ == '__main__': # 填您的密钥 clientKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # 填您自己的代理,不要填本地的地址 proxy = "http://JN3wWChA:Dsg7ckfv@176.222.59.86:64048" proxies = { 'http': proxy, 'https': proxy, } # 要访问的网址 # url = "https://apkpure.com/" url = "https://billetterie.psg.fr/" # 正常情况是这样的:使用普通requests请求 # import requests # response = requests.get(url, proxies=proxies) # print("请求响应:", response.status_code) # print("网页标题:", re.search(r"<title>(.*?)</title>", response.text)[0]) # >>> 请求响应: 403 # >>> 网页标题: <title>Attention Required! | Cloudflare</title> # 使用 curl_cffi.requests请求 # from curl_cffi import requests # response = requests.get(url, proxies=proxies, impersonate="chrome110") # print("请求响应:", response.status_code) # print("网页标题:", re.search(r"<title>(.*?)</title>", response.text)[0]) # >>> 如果使用curl_cffi就可以正常请求,说明并没有开启严格的5s盾 # >>> 请求响应: 403 # >>> 网页标题: <title>Just a moment...</title> # 使用接口返回的值来请求 # 创建任务 task = get_result(url, proxy=proxy) if not task.get("solution"): print("任务失败", task) exit() # 获取结果 solution = task.get("solution") headers = solution.get("request_headers") headers.update(solution.get("headers")) print("Headers", headers) cookies = solution.get("cookies") print("Cookies:", cookies) from curl_cffi import requests # 这里也配套指定impersonate="chrome120" response = requests.get(url, headers=headers, cookies=cookies, proxies=proxies, impersonate="chrome120") print("请求响应:", response.status_code) print("网页标题:", re.search(r"<title>(.*?)</title>", response.text)[0]) |