Cloudflare: giải quyết Thử thách
MẸO
Tạo tác vụ bằng phương pháp createTask và nhận kết quả bằng phương pháp getTaskResult.
Lưu ý
- Proxy là cần thiết, vui lòng sử dụng Static proxy hoặc Sticky proxy thay vì Rotating proxy.
 - Hỗ trợ userAgent tùy chỉnh, vui lòng giữ nguyên userAgent bạn đang sử dụng.
 - Nếu bạn không nhận được giải pháp, IP của bạn có thể bị chặn, vui lòng thử đổi proxy.
 - Bạn phải sử dụng thư viện yêu cầu TLS để yêu cầu trang web mục tiêu.
 
Loại tác vụ type như sau
AntiCloudflareTask
Tạo tác vụ
Tạo tác vụ bằng createTask.
Cấu trúc đối tượng tác vụ
| Thuộc tính | Loại | Bắt buộc | Mô tả | 
|---|---|---|---|
| type | String | Bắt buộc | AntiCloudflareTask | 
| websiteURL | String | Bắt buộc | Địa chỉ của trang mục tiêu. | 
| proxy | String | Bắt buộc | Static proxy hoặc Sticky proxy của bạn. Tìm hiểu Sử dụng proxy | 
| userAgent | String | Tùy chọn | user-agent bạn đã sử dụng để yêu cầu trang web mục tiêu. Chỉ hỗ trợ userAgent của Chrome | 
| html | String | Tùy chọn | Phản hồi khi yêu cầu trang web mục tiêu, thường chứa “Just a moment…” và mã trạng thái là 403. Một số trang web yêu cầu HTML này. | 
Ví dụ yêu cầu
POST https://api.capsolver.com/createTask
Host: api.capsolver.com
Content-Type: application/json{
  "clientKey": "YOUR_API_KEY",
  "task": {
    "type": "AntiCloudflareTask",
    "websiteURL": "https://www.yourwebsite.com",
    "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36",
    "html": "<!DOCTYPE html><html lang=\"en-US\"><head><title>Just a moment...</title>...",
    "proxy": "ip:port:user:pass"
  }
}Ví dụ phản hồi
{
  "errorId": 0,
  "status": "idle",
  "taskId": "df944101-64ac-468d-bc9f-41baecc3b8ca"
}
 Nhận kết quả
Sử dụng phương thức getTaskResult để nhận kết quả nhận dạng
Tùy thuộc vào trang web và proxy, bạn sẽ nhận được kết quả trong khoảng thời gian từ 2s đến 20s
Ví dụ yêu cầu
POST https://api.capsolver.com/getTaskResult
Host: api.capsolver.com
Content-Type: application/json{
  "clientKey": "YOUR_API_KEY",
  "taskId": "df944101-64ac-468d-bc9f-41baecc3b8ca"
}Ví dụ phản hồi
{
  "errorId": 0,
  "taskId": "df944101-64ac-468d-bc9f-41baecc3b8ca",
  "status": "ready",
  "errorCode": "",
  "errorDescription": "",
  "solution": {
    "cookies": {
        "cf_clearance": "Bcg6jNLzTVaa3IsFhtDI.e4_LX8p7q7zFYHF7wiHPo...uya1bbdfwBEi3tNNQpc"
    },
    "token": "Bcg6jNLzTVaa3IsFhtDI.e4_LX8p7q7zFYHF7wiHPo...uya1bbdfwBEi3tNNQpc",
    "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36"
  }
}Sử dụng yêu cầu SDK
# pip install --upgrade capsolver
# export CAPSOLVER_API_KEY='...'
 
import capsolver
 
# capsolver.api_key = "..."
solution = capsolver.solve({
  "type": "AntiCloudflareTask",
  "websiteURL": "https://www.yourwebsite.com",
  "proxy": "ip:port:user:pass"
})Mã mẫu
# pip install requests
import requests
import time
 
api_key = "YOUR_API_KEY"  # your api key of capsolver
 
def capsolver():
    payload = {
        "clientKey": api_key,
        "task": {
            "type": "AntiCloudflareTask",
            "websiteURL": "https://www.yourwebsite.com",
            "proxy": "ip:port:user:pass"
        }
    }
    res = requests.post("https://api.capsolver.com/createTask", json=payload)
    resp = res.json()
    task_id = resp.get("taskId")
    if not task_id:
        print("Failed to create task:", res.text)
        return
    print(f"Got taskId: {task_id} / Getting result...")
 
    while True:
        time.sleep(1)  # delay
        payload = {"clientKey": api_key, "taskId": task_id}
        res = requests.post("https://api.capsolver.com/getTaskResult", json=payload)
        resp = res.json()
        status = resp.get("status")
        if status == "ready":
            return resp.get("solution", {})
        if status == "failed" or resp.get("errorId"):
            print("Solve failed! response:", res.text)
            return
 
token = capsolver()
print(token)