Solve/bypass recaptcha using 2captcha API || python tutorial - CodeLogic

Solve/bypass recaptcha using 2captcha API || python tutorial

We code with logic

Solve/bypass recaptcha using 2captcha API || python tutorial

solve captcha

Did you know? We can create a robot than can prove to any website that it is not a robot, lol.
I was trying to automate a website. But there was a problem, that it was showing captchas. Then I found 2captcha.com. We can use their API to solve recaptcha automatically.

How it works

Solve reCaptcha

Before we start coding, let’s see how does it work.
Every website which shows a captcha have a site-key same as any API key. And there is an input as a form have(but invisible). When you solve the captcha answer key is generated and filled in the input. When we click on submit button the answer key is verified and we can move ahead.

Here we will see how to get that answer key automatically without solving any captcha.

How 2Captcha works

Solving captcha/reCaptcha with 2captchas is not that hard. 2Captcha have some workers, who solves captcha to earn money(and here we pay them for solving captcha).
So, then? We first get the site key from the website. For example

site_key = driver.find_element_by_css_selector('.g-recaptcha').get_attribute('data-sitekey')

This site key, with url of page with captcha is sent to 2Captchas via API. Then with that site-key they request a recaptcha and a worker solves the recaptcha for us and return an answer(key). We put that key in the input(for answer key) and click on submit button.
NOTE: for some cases there is no submit button, but there is a callback action we can call that javascript function with “driver.execute_script” method.

Solve reCAPTCHA

First of all you will need to sign up to 2captcha.com and get an API key. After that let’s do some coding(my favorite part). We will here create a method/function to get the answer from 2Captcha.com.

#Variables
API_KEY = #Your API key here
URL = f'https://2captcha.com/in.php?key={API_KEY}&method=userrecaptcha&googlekey=__key__&pageurl=__url__'
#--------

def GetAnswer(site_key, site_url):
     """Sends captcha to 2captcha solver, checks every 5 sec if captcha is solved
     once solved returns the answer key"""

     m = requests.get(URL.replace('__key__', site_key).replace('__url__', page_url))
     code = m.text.split('|')[-1]
     print("captcha sent to 2captcha")
     print("waiting for answer…", end='')
     while True:
         time.sleep(5)
         resp = requests.get(f'https://2captcha.com/res.php?key={API_KEY}&id={code}&action=get')
         try:
             result = resp.text.split('|')[-1]
             if len(result) > 100:
                 print('done')
                 return result
         except:
             pass

Explanation:
In this function two arguments are passed (1. site_key, 2. page_url). As we talked earlier the “site_key” here is like a question and we will get its answer from 2Captcha. So, here it create a URL with all data and create a get request to 2Captcha. In response, we get a code(numerical) which is used to retrieve the answer. Again, we make a get request but this time on res.php to get the result. If any worker on 2Captcha.com solves the captcha we will receive answer key or else and error message saying “captcha not solved”. If captcha is not solved we will wait for 5 sec and again check is captcha is solved. Once solved we get the answer key.
Now what to do with answer key?

Here is some more code for an example on how to solve recaptcha.

key = driver.find_element_by_css_selector('.g-recaptcha').get_attribute("data-sitekey")
answer_key = GetAnswer(key, driver.current_url)

#Find the text area of recaptcha answer
ans_field = driver.find_element_by_name('g-recaptcha-response')

#Put the answer key inside of text-area
driver.execute_script([arguments[0].innerHTML=arguments[1], ans_field, answer_key)

That’s all.
You have bypassed the captcha.

Follow @codelogic.py on Instagram for daily updates

More tutorials here

 

Leave a Reply

Your email address will not be published. Required fields are marked *