DEV Community

Cover image for A Beginner's Guide to Capsolver: Using Python 3 for Captcha Tasks
Jairon Landa
Jairon Landa

Posted on

A Beginner's Guide to Capsolver: Using Python 3 for Captcha Tasks

If you're interested in understanding how to solve complex tasks such as image recognition, voice recognition, and captchas, you're in the right place. Capsolver is a powerful platform designed to provide solutions for such tasks, and this blog will guide you on how to get started using Python 3.

Step 1: Create an Account

Before you can unlock the capabilities of Capsolver, you'll need to create an account. The process is straightforward. Simply navigate to the user panel on the website and follow the prompts for registration. There are different methods to register, and if you stumble upon any issues or prefer a particular method, our dedicated support team is always ready to assist you.

Step 2: Obtain Your API Key

CapSolver Dashboard

Upon successful registration, the next step is to retrieve your API key. This key is instrumental as it authenticates your requests to Capsolver's API. To access it, navigate to the home page panel. As with any sensitive information, ensure to keep your API key secure and refrain from sharing it with unauthorized parties.

Step 3: Install the Official Capsolver SDK (Python)

Having secured your API key, it's now time to get the Capsolver Software Development Kit (SDK). The SDK allows you to integrate Capsolver into your Python projects. It can be obtained directly from Capsolver or through third-party libraries available on PyPI. While the third-party libraries may offer additional functionalities, Capsolver cannot vouch for the safety and security of these external libraries.

For those who are interested in exploring third-party libraries, here are a few options:

  1. python3-captchaai by AndreiDrang
  2. captchaai_python by alperensert
  3. Captcha-Tools by Matthew17-21

Despite the potential additional features of these libraries, we encourage you to take the necessary precautions as Capsolver can't guarantee their safety and security.

You can download the official Capsolver SDK from GitHub through this link: https://github.com/capsolver/capsolver-python. If you prefer, you can also install the SDK via pip using the command pip install capsolver.

For this tutorial, we will focus solely on the official Capsolver SDK to solve various tasks, including image recognition, voice recognition, and captchas.

Step 4: Running the Program

Capsolver supports multiple types of tasks, each requiring a distinct approach for resolution. Let's dive into two common task types: Image Recognition and Captcha Token.

Image Recognition Task

CapSolver Image Recognition Task

For captchas involving image recognition, you'll use the Image Recognition task type. Below is a sample code to solve an Image Recognition task using the HCaptchaClassification type:

import capsolver
capsolver.api_key = "<API_KEY>"
img_path = os.path.join(Path(__file__).resolve().parent, "squirrel.jpg")
with open(img_path, 'rb') as f:
    solution = capsolver.solve({
        "type": "HCaptchaClassification",
        "question": "Please click on the squirrel",
        "queries": [
            "/9j/4AAQS.....",
            "/9j/4AAQ1.....",
            "/9j/4AAQ2.....",
            "/9j/4AAQ3.....",
            "/9j/4AAQ4.....",
        ]
    })
    print(solution)
Enter fullscreen mode Exit fullscreen mode

Please replace <API_KEY> with your actual API key.
Here's a simple explanation of the code above:

  • Type: This property identifies the type of captcha to be solved. In this example, we are using HCaptchaClassification to solve the HCaptcha captcha. Ensure that you are using the correct value before making a request.

  • Question: This property is used to set the task. Please note, Capsolver currently only supports English. If you need to convert to another language, you must do so before making the request. Set the value as requested.

  • Queries: This property requires a list of base64 encoded images, excluding the "data/image" part.

For more detailed information about these parameters, you can check the documentation here: https://docs.capsolver.com/guide/recognition/HCaptchaClassification.html#create-task

Captcha Token Task

For captchas that require a token, often encountered in automation tools and RPA, you will need to use the Captcha Token task type. Here's an example of how to use the SDK for a Captcha Token task using the HCaptchaTaskProxyLess type:

# capsolver.api_key = "..."
solution = capsolver.solve({
    "type": "HCaptchaTaskProxyLess",
    "websiteURL": "https://accounts.hcaptcha.com/demo",
    "websiteKey": "a5f74b19-9e45-40e0-b45d-47ff91b7a6c2",
})
Enter fullscreen mode Exit fullscreen mode

Please ensure your API KEY is correct; otherwise, it will not work. Let's breakdown the elements of the code:

  • Type: This parameter specifies the type of captcha to be solved. In this instance, we're using HCaptchaTaskProxyLess to solve the HCaptcha captcha. Ensure you're using the correct value before making a request.

  • websiteURL: This parameter is necessary as it denotes the website that is using HCaptcha. In our example, we've used the Hcaptcha demo site.

  • websiteKey: This represents the domain's public key, which is rarely updated.

For more detailed information about these parameters, you can visit the official Capsolver documentation here: https://docs.capsolver.com/guide/captcha/HCaptcha.html#create-task

Congratulations on making it this far! You've successfully learned how to register an account with Capsolver, obtain your API key, install the official Capsolver SDK, and apply it to solve various types of captchas using Python 3. If you encounter any difficulties or have any questions, don't hesitate to contact the Capsolver support team for help. Happy coding! 🎉

Top comments (0)