Using 2captcha with Python
Asked Answered
P

1

8

I'm totally new with 2captchas and Python, so I'm trying to figure out how these two works. For now I'm working on a python script and running it on spyder to resolve images captcha. My code (using 2captcha API) returns html of the site in return response. It tries to sign up for a site and in return fails the main task that is to resolve captcha. My code looks something like this

import requests
from time import sleep

API_KEY = '2captchaapi'  # Your 2captcha API KEY
site_key = '2captcha site key'  # site-key, read the 2captcha docs on how to get this
url = 'site'  # example url
proxy = 'proxy'  # example proxy

proxy = {'http': 'http://' + proxy, 'https': 'https://' + proxy}

s = requests.Session()


# here we post site key to 2captcha to get captcha ID (and we parse it here too)
captcha_id = s.post(
    "http://2captcha.com/in.php?key={}&method=userrecaptcha&googlekey={}&pageurl={}".format(API_KEY, site_key, url), proxies=proxy).text.split('|')[1]
# then we parse gresponse from 2captcha response
recaptcha_answer = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(API_KEY, captcha_id), proxies=proxy).text
print("solving ref captcha...")
while 'CAPCHA_NOT_READY' in recaptcha_answer:
    sleep(5)
    recaptcha_answer = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(API_KEY, captcha_id), proxies=proxy).text

recaptcha_answer = recaptcha_answer.split('|')[1]

print(recaptcha_answer)

payload = {
    'signup-form[votes]':                       '',
    'signin-form[subs]':                        '',
    'signin-form[post_referer]':                'site',
    'signup-form[step2][hidden_captcha]':       '',
    'signup-form[details][login]':              '[email protected]',
    'signup-form[details][profile_name]':       'name1',
    'signup-form[details][password]':           'secret44%',
    'signup-form[details][password_confirm]':   'secret44%',
    'signup-form[details][tos_pp]':             'on',
    'signup-form[step2][optional][age]':        '24',
    'signup-form[step2][optional][sex]':        'Man',
    'signup-form[step2][optional][country]':    'france',
    'signup-form[step2][optional][language]':   'french',
    'signup-form[step2][profilepic][file]':     '',
    'g-recaptcha-response':                     recaptcha_answer
}


# then send the post request to the url
response = s.post(url, payload, verify=False)


print(response.text)

Please let me know how can I solve Image captchas using this code and if I'm using the right tools to solve this captcha challenge.

Pacification answered 28/2, 2019 at 17:30 Comment(0)
M
3

You can use this python library - 2captcha-python. You want to extract the text from the image captcha so you can use the following code after installation and configuration steps.

To bypass a normal captcha (distorted text on image) use the following method. This method also can be used to recognize any text on the image.

result = solver.normal('path/to/captcha.jpg', param1=..., ...)
Myology answered 21/10, 2021 at 6:54 Comment(1)
Thanks a lot! Also, could you please tell me how one could solve this kind of captcha/puzzle?Algebraist

© 2022 - 2024 — McMap. All rights reserved.