You are currently viewing How To Solve CAPTCHA In A Few Steps In Python Using 2captcha

How To Solve CAPTCHA In A Few Steps In Python Using 2captcha

You may have solved millions of captchas for verification, which can be a tedious chore when you need to authenticate your identity as a human. These websites offer an additional layer of security by utilizing captcha services to prevent bots and automation scripts from accessing their website.

What is CAPTCHA?

CAPTCHA (Completely Automated Public Turing Test to Tell Computers and Humans Apart) is a form of challenge-response test used to assess whether or not a user is human.

CAPTCHAs display jumbled, scrambled, or distorted text, images, numbers, and audio that is tough for computers to solve but relatively easy for people to solve. These are used to protect websites and applications against harmful actions.

2captcha API

There are numerous applications and services available to assist us in solving a captcha in seconds. 2captcha is a website that offers captcha-solving services.

2captcha provides API for various programming languages, including Python, and we’ll utilize the 2captcha-python library to solve the captcha in a few steps.

Installing dependencies

The primary work is to install the Python library called 2captcha-python. Run the following command in your terminal.

Please keep in mind that we are using pip to install the library. If you are not using pip, the installation command will be different.

Solving reCAPTCHA

We’ll use a reCAPTCHA testing website and here’s the URL of the website https://patrickhlauke.github.io/recaptcha/. It provides a reCAPTCHA widget for testing purposes.

reCAPTCHA testing website

Code

First, we must import the library and create an instance of the class TwoCaptcha, which we will initialise with the API key. To make a POST request to the target site, we also need the requests library.

After we’ve built the instance, the next step is to obtain the token from the 2captcha server.

We called the recaptcha method from the instance captcha_solver within the try block and passed the reCAPTCHA data-sitekey to the sitekey parameter and the URL of the target site to the url parameter.

To get the sitekey from the website, open it in inspect mode and look for the tag <iframe> with title="reCAPTCHA" and you’ll find the key within the src attribute.

reCAPTCHA sitekey

If any exceptions are triggered within the except block, the program will display an error message and exit. If the program is successfully executed, it will return the token that will be used to solve reCAPTCHA and then quit.

The final step will be to transfer the token to the target site, which will be accomplished by sending a POST request to the target site via the requests library.

We changed the else part of the code, and the result is the code seen above. Before performing the POST request to the target site, we’ve established certain configurations to send along with the request within the else block.

We used the requests library’s post function to send a POST request to the target site, passing the site_url (target site URL), headers (containing the user-agent), and data (containing token). The data can contain more parameters depending on the form.

Note: The received token from the 2captcha servers will only be valid for 120 seconds, so you need to submit the token to the target site within the time limit.

Solving hCAPTCHA

The URL of the hCAPTCHA demo site is https://accounts.hcaptcha.com/demo.

hCAPTCHA Demo site

The procedure will be the same as with reCAPTCHA, with the exception that we must modify the method to hcaptcha from the 2captcha-python package.

We’ll get the sitekey of the hCAPTCHA within the src attribute of the <iframe> tag.

hCAPTCHA sitekey

Conclusion

We’ve used the 2captcha-python package for solving the reCAPTCHA and hCAPTCHA. The 2captcha-python package provided by 2captcha provides captcha-solving services and almost all types of captcha can be solved using their APIs. They provide a browser extension for solving CAPTCHAs.


That’s all for now

Keep Coding✌✌