2Captcha Python Client
The 2captcha-python library provides an easy-to-use Python module for integrating with the 2Captcha API. It simplifies the process of automating captcha solving for various types like reCAPTCHA, Arkose, GeeTest, and image-to-text captchas. Currently at version 2.0.6, the library maintains an active release cadence, regularly adding support for new captcha types and enhancing existing features.
Warnings
- breaking Version 2.0.0 introduced asynchronous API support, requiring the use of `AsyncTwoCaptcha` and `await` for async operations. While synchronous methods (`TwoCaptcha`) are still available, users transitioning to async workflows must update their code accordingly.
- deprecated The underlying 2Captcha API v1 is deprecated. All new features and improvements are being added exclusively to API v2. While API v1 remains supported for compatibility, migrating to API v2 (which `2captcha-python` v2.x utilizes) is highly recommended to access the latest functionalities and ensure future compatibility.
- gotcha Using the incorrect captcha type or providing wrong parameters for a specific captcha will lead to failed solutions and wasted funds. 2Captcha supports numerous captcha types, each requiring a specific API method and set of parameters.
- gotcha Inadequate error handling can lead to silent failures or unhandled exceptions. The library throws specific exceptions (`ValidationException`, `NetworkException`, `ApiException`) for different error scenarios from the API or network issues.
- gotcha Poor account balance management and inefficient polling logic can quickly drain your budget or lead to rate limiting. Polling for results too frequently (e.g., every 100ms) can trigger `MAX_USER_TURN` errors, while not monitoring your balance can lead to service interruptions.
- gotcha Hardcoding your 2Captcha API key directly in your scripts poses a security risk. If your code is exposed, your API key can be compromised.
Install
-
pip install 2captcha-python
Imports
- TwoCaptcha
from twocaptcha import TwoCaptcha
- AsyncTwoCaptcha
from twocaptcha import AsyncTwoCaptcha
Quickstart
import os
from twocaptcha import TwoCaptcha
from twocaptcha.solver import ValidationException, NetworkException, ApiException
# Best practice: Load API key from environment variables
API_KEY = os.environ.get('TWO_CAPTCHA_API_KEY', 'YOUR_2CAPTCHA_API_KEY')
solver = TwoCaptcha(API_KEY)
try:
# Example: Solving a normal image-to-text captcha
# Replace 'path/to/captcha.jpg' with your captcha image file path or URL
# You can also pass base64 encoded image data
result = solver.normal('path/to/captcha.jpg')
print(f"Captcha solved: {result['code']}")
# The 'id' field can be used to report incorrect solutions later
# print(f"Captcha ID: {result['id']}")
except ValidationException as e:
print(f"Validation error: {e}")
except NetworkException as e:
print(f"Network error: {e}")
except ApiException as e:
print(f"API error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")