Welcome to python-anticaptcha’s documentation!

Client library for solve captchas with Anticaptcha.com support. The library supports both Python 2.7 and Python 3.

The library is cyclically and automatically tested for proper operation. We are constantly making the best efforts for its effective operation.

In case of any problems with integration - read the documentation, create an issue, use Gitter or contact privately.

Usage

To use this library do you need Anticaptcha.com API key.

Solve recaptcha

Example snippet for Recaptcha:

from python_anticaptcha import AnticaptchaClient, NoCaptchaTaskProxylessTask

api_key = '174faff8fbc769e94a5862391ecfd010'
site_key = '6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-'  # grab from site
url = 'https://www.google.com/recaptcha/api2/demo'

client = AnticaptchaClient(api_key)
task = NoCaptchaTaskProxylessTask(url, site_key)
job = client.createTask(task)
job.join()
print job.get_solution_response()

The full integration example is available in file examples/recaptcha.py.

If you only process few page many times to increase reliability, you can specify whether the captcha is visible or not. This parameter is not required, as is the system detects invisible sitekeys automatically, and needs several recursive measures for automated training and analysis. For provide that pass is_invisible parameter to NoCaptchaTaskProxylessTask or NoCaptchaTask eg.:

from python_anticaptcha import AnticaptchaClient, NoCaptchaTaskProxylessTask

api_key = '174faff8fbc769e94a5862391ecfd010'
site_key = '6Lc-0DYUAAAAAOPM3RGobCfKjIE5STmzvZfHbbNx'  # grab from site
url = 'https://losangeles.craigslist.org/lac/kid/d/housekeeper-sitting-pet-care/6720136191.html'

client = AnticaptchaClient(api_key)
task = NoCaptchaTaskProxylessTask(url, site_key, is_invisible=True)
job = client.createTask(task)
job.join()
print job.get_solution_response()

Solve text captcha

Example snippet for text captcha:

from python_anticaptcha import AnticaptchaClient, ImageToTextTask

api_key = '174faff8fbc769e94a5862391ecfd010'
captcha_fp = open('examples/captcha_ms.jpeg', 'rb')
client = AnticaptchaClient(api_key)
task = ImageToTextTask(captcha_fp)
job = client.createTask(task)
job.join()
print job.get_captcha_text()

Solve funcaptcha

Example snippet for funcaptcha:

from python_anticaptcha import AnticaptchaClient, FunCaptchaTask, Proxy
UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 ' \
     '(KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'

api_key = '174faff8fbc769e94a5862391ecfd010'
site_key = 'DE0B0BB7-1EE4-4D70-1853-31B835D4506B'  # grab from site
url = 'https://www.google.com/recaptcha/api2/demo'
proxy = Proxy.parse_url("socks5://login:password@123.123.123.123")

client = AnticaptchaClient(api_key)
task = FunCaptchaTask(url, site_key, proxy=proxy, user_agent=user_agent)
job = client.createTask(task)
job.join()
print job.get_token_response()

Report incorrect image

Example snippet for reporting an incorrect image task:

from python_anticaptcha import AnticaptchaClient, ImageToTextTask

api_key = '174faff8fbc769e94a5862391ecfd010'
captcha_fp = open('examples/captcha_ms.jpeg', 'rb')
client = AnticaptchaClient(api_key)
task = ImageToTextTask(captcha_fp)
job = client.createTask(task)
job.join()
print job.get_captcha_text()
job.report_incorrect()

Setup proxy

The library is not responsible for managing the proxy server. However, we point to the possibility of simply launching such a server by:

pip install mitmproxy
mitmweb -p 9190 -b 0.0.0.0 --ignore '.' --socks

Next to in your application use something like:

proxy = Proxy.parse_url("socks5://123.123.123.123:9190")

We recommend entering IP-based access control for incoming addresses to proxy. IP address required by Anticaptcha.com is:

69.65.41.21
209.212.146.168

Error handling

In the event of an application error, the AnticaptchaException exception is thrown. To handle the exception, do the following:

from python_anticaptcha import AnticatpchaException, ImageToTextTask

try:
    # any actions
except AnticatpchaException as e:
    if e.error_code == 'ERROR_ZERO_BALANCE':
        notify_about_no_funds(e.error_id, e.error_code, e.error_description)
    else:
        raise

API

Base

class python_anticaptcha.base.AnticaptchaClient(client_key, language_pool='en', host='api.anti-captcha.com', use_ssl=True)[source]
APP_STAT_URL = '/getAppStats'
BALANCE_URL = '/getBalance'
CREATE_TASK_URL = '/createTask'
REPORT_IMAGE_URL = '/reportIncorrectImageCaptcha'
REPORT_RECAPTCHA_URL = '/reportIncorrectRecaptcha'
SOFT_ID = 847
TASK_RESULT_URL = '/getTaskResult'
client_ip
client_key = None
createTask(task)[source]
createTaskSmee(task, timeout=300)[source]

Beta method to stream response from smee.io

getAppStats(soft_id, mode)[source]
getBalance()[source]
getTaskResult(task_id)[source]
language_pool = 'en'
reportIncorrectImage(task_id)[source]
reportIncorrectRecaptcha(task_id)[source]
response_timeout = 5
class python_anticaptcha.base.Job(client, task_id)[source]
check_is_ready()[source]
client = None
get_answers()[source]
get_captcha_text()[source]
get_cells_numbers()[source]
get_solution()[source]
get_solution_response()[source]
get_token_response()[source]
join(maximum_time=None)[source]
report_incorrect()[source]
report_incorrect_image()[source]
report_incorrect_recaptcha()[source]
task_id = None

Exceptions

exception python_anticaptcha.exceptions.AnticaptchaException(error_id, error_code, error_description, *args)[source]
python_anticaptcha.exceptions.AnticatpchaException

alias of python_anticaptcha.exceptions.AnticaptchaException

exception python_anticaptcha.exceptions.InvalidWidthException(width)[source]
exception python_anticaptcha.exceptions.MissingNameException(cls)[source]

Tasks

class python_anticaptcha.tasks.AntiGateTask(*args, **kwargs)[source]
class python_anticaptcha.tasks.AntiGateTaskProxyless(website_url, template_name, variables, *args, **kwargs)[source]
serialize(**result)[source]
templateName = None
type = 'AntiGateTask'
variables = None
websiteURL = None
class python_anticaptcha.tasks.BaseTask[source]
serialize(**result)[source]
type = None
class python_anticaptcha.tasks.CookieMixin(*args, **kwargs)[source]
serialize(**result)[source]
class python_anticaptcha.tasks.FunCaptchaProxylessTask(website_url, website_key, subdomain=None, data=None, *args, **kwargs)[source]
data = None
funcaptchaApiJSSubdomain = None
serialize(**result)[source]
type = 'FunCaptchaTaskProxyless'
websiteKey = None
websiteURL = None
class python_anticaptcha.tasks.FunCaptchaTask(*args, **kwargs)[source]
type = 'FunCaptchaTask'
class python_anticaptcha.tasks.GeeTestTask(*args, **kwargs)[source]
class python_anticaptcha.tasks.GeeTestTaskProxyless(website_url, gt, challenge, subdomain=None, lib=None, *args, **kwargs)[source]
challenge = None
geetestApiServerSubdomain = None
geetestGetLib = None
gt = None
serialize(**result)[source]
type = 'GeeTestTaskProxyless'
websiteURL = None
class python_anticaptcha.tasks.HCaptchaTask(*args, **kwargs)[source]
type = 'HCaptchaTask'
class python_anticaptcha.tasks.HCaptchaTaskProxyless(website_url, website_key, *args, **kwargs)[source]
serialize(**result)[source]
type = 'HCaptchaTaskProxyless'
websiteKey = None
websiteURL = None
class python_anticaptcha.tasks.ImageToTextTask(fp, phrase=None, case=None, numeric=None, math=None, min_length=None, max_length=None, comment=None, website_url=None, *args, **kwargs)[source]
case = None
comment = None
fp = None
math = None
maxLength = None
minLength = None
numeric = None
phrase = None
serialize(**result)[source]
type = 'ImageToTextTask'
websiteUrl = None
class python_anticaptcha.tasks.NoCaptchaTask(*args, **kwargs)[source]
type = 'NoCaptchaTask'
class python_anticaptcha.tasks.NoCaptchaTaskProxylessTask(website_url, website_key, website_s_token=None, is_invisible=None, recaptcha_data_s_value=None, *args, **kwargs)[source]
recaptchaDataSValue = None
serialize(**result)[source]
type = 'NoCaptchaTaskProxyless'
websiteKey = None
websiteSToken = None
websiteURL = None
class python_anticaptcha.tasks.ProxyMixin(*args, **kwargs)[source]
serialize(**result)[source]
class python_anticaptcha.tasks.RecaptchaV2EnterpriseTask(*args, **kwargs)[source]
type = 'RecaptchaV2EnterpriseTask'
class python_anticaptcha.tasks.RecaptchaV2EnterpriseTaskProxyless(website_url, website_key, enterprise_payload, api_domain, *args, **kwargs)[source]
apiDomain = None
enterprisePayload = None
serialize(**result)[source]
type = 'RecaptchaV2EnterpriseTaskProxyless'
websiteKey = None
websiteURL = None
class python_anticaptcha.tasks.RecaptchaV2Task(*args, **kwargs)[source]
type = 'RecaptchaV2Task'
class python_anticaptcha.tasks.RecaptchaV2TaskProxyless(website_url, website_key, website_s_token=None, is_invisible=None, recaptcha_data_s_value=None, *args, **kwargs)[source]
type = 'RecaptchaV2TaskProxyless'
class python_anticaptcha.tasks.RecaptchaV3TaskProxyless(website_url, website_key, min_score, page_action, is_enterprise=False, *args, **kwargs)[source]
isEnterprise = False
minScore = None
pageAction = None
serialize(**result)[source]
type = 'RecaptchaV3TaskProxyless'
websiteKey = None
websiteURL = None
class python_anticaptcha.tasks.UserAgentMixin(*args, **kwargs)[source]
serialize(**result)[source]

Changelog

1.0.0 - 2022-03-28

Added

  • Add new tasks:
    • AntiGateTask and AntiGateTaskProxyless
    • RecaptchaV2EnterpriseTask and RecaptchaV2EnterpriseTaskProxyless
    • GeeTestTask and GeeTestTaskProxyless
    • RecaptchaV2Task (alias of NoCaptchaTask) and RecaptchaV2TaskProxyless (alias of NoCaptchaTaskProxyless)
  • Add example for AntiGateTaskProxyless
  • Add optional parameters comment, websiteUrl to ImageToTextTask
  • Add optional parameter funcaptchaApiJSSubdomain, data to FunCaptchaTask*
  • Add optional parameter isEnterprise to RecaptchaV3Task*

Removed

  • Drop tasks unsupported upstream: CustomCaptchaTask, SquareNetTask

Changed

  • Internal refactor to extract UserAgentMixin, CookieMixin
  • Use nose2 for tests

0.7.1 - 2020-07-17

Added

  • Added examples for proxy mode including hcaptcha_request_proxy

Changed

  • Fix inheritance of FunCaptchaTask
  • Added FunCaptchaTask to e2e tests

0.7.0 - 2020-06-08

Added

  • Added parameter recaptchaDataSValue in NoCaptchaTask*

    Thanks to this change added support for additional “data-s” used by some custom ReCaptcha deployments, which is in fact a one-time token and must be grabbed every time you want to solve a Recaptcha. <div class=”g-recaptcha” data-sitekey=”some sitekey” data-s=”THIS_TOKEN”></div>

Changed

  • Fixed deprecated method report_incorrect. You should currently use report_incorrect_image instead already.

0.6.0 - 2020-04-13

Added

  • Added custom timeout for createTaskSmee. Use as client.createTaskSmee(task, timeout=5*60). Default timeout is 5 minutes.
  • Added squarenet_validator for usage with thread pool for concurrent execution

Changed

  • Default 5 seconds timeout apply to all API request.

0.5.1 - 2020-03-31

Changed

  • Fix import of package

0.5.0 - 2020-03-30

Added

  • Added HCaptchaTaskProxyless and HCaptchaTask for support hCaptcha . See examples/hcaptcha_request.py for detailed usage example.
  • Added SquareNetTask. See examples/squarenet.py for detailed usage example.
  • Added Job.report_incorrect_recaptcha and Job.report_incorrect_image .

Changed

  • Exposed FunCaptchaProxylessTask as python_anticaptcha.FunCaptchaProxylessTask
  • Exposed CustomCaptchaTask as python_anticaptcha.CustomCaptchaTask
  • Formated code via Black
  • Move constant monitoring to GitHub Actions
  • Deprecated Job.report_incorrect. Use report_incorrect_image instead.

0.4.2 - 2019-10-27

Added

  • Added example remote_image.py

Changed

  • Switch CI from TravisCI to GitHub Actions
  • Automate PyPI releases
  • Use use_scm_version for versioning
  • Drop use_2to3 in setup.py

0.4.1 - 2019-07-09

Added

  • Added python_anticaptcha.__version__ to provide version signature (see PEP396)

Changed

  • python_anticaptcha.AnticaptchaClient.createTaskSmee use shared session & keep connection.

0.4.0 - 2019-06-28

Added

  • Added python_anticaptcha.AnticaptchaClient.createTaskSmee to receive responses without polling
    The method, which is based on the callback result of captcha / task factory to Smee.io, which immediately transfers it to library. Allows to significantly shorten the waiting time for a response and to reduce load the network connection. The method is in beta and the way it works may change. All comments are welcome.
  • Recaptcha V3 is now officially supported by Anti-Captcha. Added python_anticaptcha.RecaptchaV3TaskProxyless.

0.3.2 - 2018-10-17

Added

  • Added support for IsInvisible flag in NoCaptchaTaskProxylessTask and NoCaptchaTask

0.3.1 - 2018-03-18

Changed

  • Replaced python_anticaptcha.AnticatpchaException to python_anticaptcha.AnticaptchaException due typo

Added

  • Added python_anticaptcha.exceptions.AnticatpchaException
  • Added docs about error handling

Removed

  • Deprecated python_anticaptcha.exceptions.AnticatpchaException

Development

The project is open-source.

Changes are managed through GitHub. Pull requests are particularly welcome.

All changes are automatically tested using TravisCI.

New release

Follow these steps to publish the new release:

  • update changelog - use any text editor
  • bump version - use `bumpversion {major,minor,patch}`
  • build package - use `python setup.py sdist bdist_wheel --universal`
  • upload release to PyPI - use `twine upload dist/*`
  • push changes to GitHub - `git push origin && git push --tags`

Indices and tables