CAPSOLVER
Blog
How to solve Cloudflare Turnstile Captcha with Python

How to solve Cloudflare Turnstile Captcha with Python

Logo of CapSolver

Sora Fujimoto

AI Solutions Architect

13-May-2024

Solve Cloudflare with Python, Cloudflare python solver

What is Cloudflare Turnstile

Cloudflare Turnstile is a free tool to replace CAPTCHAs
Turnstile delivers frustration-free, CAPTCHA-free web experiences to website visitors - with just a simple snippet of free code. Moreover, Turnstile stops abuse and confirms visitors are real without the data privacy concerns or awful user experience of CAPTCHAs.

How to identify Cloudflare Turnstile Captcha

🛠️ Solving cloudflare turnstile captcha

⚙️ Prerequisites

🤖 Step 1: Install Necessary Packages

Execute the following commands to install the required packages:

python Copy
pip install requests

👨‍💻 Step 2: Python Code for solve Cloudflare Turnstile Captcha

Here's a Python sample script to accomplish the task:

python Copy
import time
import requests

CAPSOLVER_API_KEY = "api key"
PAGE_URL = "url"
WEBSITE_KEY = "site key"

def solvecf(metadata_action=None, metadata_cdata=None):
    url = "https://api.capsolver.com/createTask"
    task = {
        "type": "AntiTurnstileTaskProxyLess",
        "websiteURL": PAGE_URL,
        "websiteKey": WEBSITE_KEY,
    }
    if metadata_action or metadata_cdata:
        task["metadata"] = {}
        if metadata_action:
            task["metadata"]["action"] = metadata_action
        if metadata_cdata:
            task["metadata"]["cdata"] = metadata_cdata
    data = {
        "clientKey": CAPSOLVER_API_KEY,
        "task": task
    }
    response_data = requests.post(url, json=data).json()
    print(response_data)
    return response_data['taskId']


def solutionGet(taskId):
    url = "https://api.capsolver.com/getTaskResult"
    status = ""
    while status != "ready":
        data = {"clientKey": CAPSOLVER_API_KEY, "taskId": taskId}
        response_data = requests.post(url, json=data).json()
        print(response_data)
        status = response_data.get('status', '')
        print(status)
        if status == "ready":
            return response_data['solution']

        time.sleep(2)


def main():
    
    taskId = solvecf()
    solution = solutionGet(taskId)
    if solution:
        user_agent = solution['userAgent']
        token = solution['token']

    print("User_Agent:", user_agent)
    print("Solved Turnstile Captcha, token:", token)

  
if __name__ == "__main__":
    main()

⚠️ Change these variables

  • CAPSOLVER_API_KEY: Obtain your API key from the Capsolver Dashboard.
  • PAGE_URL: Replace with the URL of the website for which you wish to solve the CloudFlare Turnstile Captcha.
  • WEBSITE_KEY: Replace with the site key of the website

Compliance Disclaimer: The information provided on this blog is for informational purposes only. CapSolver is committed to compliance with all applicable laws and regulations. The use of the CapSolver network for illegal, fraudulent, or abusive activities is strictly prohibited and will be investigated. Our captcha-solving solutions enhance user experience while ensuring 100% compliance in helping solve captcha difficulties during public data crawling. We encourage responsible use of our services. For more information, please visit our Terms of Service and Privacy Policy.

More

How to Bypass Cloudflare Challenge While Web Scraping in 2025
How to Bypass Cloudflare Challenge While Web Scraping in 2025

Learn how to bypass Cloudflare Challenge and Turnstile in 2025 for seamless web scraping. Discover Capsolver integration, TLS fingerprinting tips, and fixes for common errors to avoid CAPTCHA hell. Save time and scale your data extraction.

Cloudflare
Logo of CapSolver

AloĂ­sio VĂ­tor

23-Jan-2025

How to Fix Cloudflare Errors 1006, 1007, and 1008 Quickly
How to Fix Cloudflare Errors 1006, 1007, and 1008 Quickly

Cloudflare errors 1006, 1007, and 1008 can block your access due to suspicious or automated traffic. Learn quick fixes using premium proxies, user agent rotation, human behavior simulation, and IP address changes to overcome these roadblocks for smooth web scraping.

Cloudflare
Logo of CapSolver

Ethan Collins

23-Jan-2025

How to Solve Cloudflare Turnstile CAPTCHA by Extension
How to Solve Cloudflare Turnstile CAPTCHA by Extension

Learn how to bypass Cloudflare Turnstile CAPTCHA with Capsolver’s extension. Install guides for Chrome, Firefox, and automation tools like Puppeteer.

Cloudflare
Logo of CapSolver

Adélia Cruz

23-Jan-2025

How to Solve Cloudflare by Using Python and Go in 2025
How to Solve Cloudflare by Using Python and Go in 2025

Will share insights on what Cloudflare Turnstile is, using Python and Go for these tasks, whether Turnstile can detect Python scrapers, and how to effectively it using solutions like CapSolver.

Cloudflare
Logo of CapSolver

Lucas Mitchell

05-Nov-2024

How to Solve Cloudflare Turnstile Captchas With Selenium
How to Solve Cloudflare Turnstile Captchas With Selenium

In this blog, we’ll discuss several effective techniques for overcoming Cloudflare Turnstile Captchas using Selenium

Cloudflare
Logo of CapSolver

Ethan Collins

11-Oct-2024

A Guide to Solving Cloudflare Turnstile Captchas in Java for Web Automation
A Guide to Solving Cloudflare Turnstile Captchas in Java for Web Automation

Learn how to automate web interactions in Java and bypass Cloudflare Turnstile CAPTCHA with practical tools and coding techniques.

Cloudflare
Logo of CapSolver

Lucas Mitchell

08-Oct-2024