DEV Community

Zeeshan
Zeeshan

Posted on • Edited on • Originally published at acidop.codes

How to Use 2Captcha to Solve reCaptcha V3 Challenges in Python

Introduction

Are you tired of being locked out of websites that use reCaptcha V3?

Don't worry boys, for we have a solution! In this blog, we'll teach you how to use Python and the 2Captcha library to solve reCaptcha V3 challenges.

But first, let's talk about why reCaptcha V3 is such a pain in the bot. Unlike previous versions of reCaptcha, V3 doesn't require users to solve a visual puzzle. Instead, it assigns a score to each user based on their browsing behavior, and if their score falls below a certain threshold, they'll be prompted to solve a challenge.

This is a problem for bots and automation tools because they don't behave like normal users. Even if you try to mimic human behavior, your score might still be too low to bypass the challenge. That's where 2Captcha comes in.

What is 2Captcha?

2Captcha is a third-party service that uses human workers to solve challenges like reCaptcha. It's like outsourcing your captcha-solving to an army of freelancers.

The process is simple and lightning fast 🔥:

  1. Captcha sent to them (captcha -> 2Captcha)
  2. They send it to a worker (2Captcha -> worker)
  3. A worker solves it (worker -> 2Captcha)
  4. The solution is sent to you (2Captcha -> you)

You can use 2Captcha for free, but you'll need to pay for credits to access the service. The cost varies depending on the number of credits you buy, but it's generally affordable. And it's a small price to pay for the convenience of not having to solve reCaptcha challenges yourself.

Setting up the 2Captcha API

  1. To use 2Captcha, you'll need to create an account and get an API key. Once you have your API key, you can use the 2Captcha Python library to send challenges to the service and get the solutions.

  2. Install the 2Captcha Python library:

pip3 install 2captcha-python
Enter fullscreen mode Exit fullscreen mode
  1. Create a new Python file and import the 2Captcha library:
import twocaptcha

api_key = 'YOUR_API_KEY_HERE'
solver = twocaptcha.TwoCaptcha(api_key)
Enter fullscreen mode Exit fullscreen mode

Getting the Sitekey and Action

Before you can solve a reCaptcha challenge, you'll need to know the sitekey and action. These values are embedded in the HTML of the website you're trying to access.

P.S. This is the boring and hard part. I promise it gets better from here 😅.

To get the sitekey and action, you can use regular expressions to search for them in the HTML. Here's some sample code that shows you how to do that:

import requests
import re

url = 'https://www.example.com'
html = requests.get(url).text

# Set the regex patterns
sitekey_pattern = re.compile(r"data-sitekey='(.*?)'")
action_pattern = re.compile(r'action="(.*?)"')

# Search the HTML for the sitekey and action
sitekey_match = sitekey_pattern.search(html)
action_match = action_pattern.search(html)

# Check if the sitekey and action were found
if sitekey_match:
    sitekey = sitekey_match.group(1)
else:
    sitekey = None

if action_match:
    action = action_match.group(1)
else:
    action = None

# Error & exit if the sitekey or action were not found
if (sitekey == None) or (action == None):
    print("Error: Sitekey or Action not found ❌")
    print(f"Sitekey: {sitekey} | Action: {action}")
    exit()
Enter fullscreen mode Exit fullscreen mode

The code assumes that the website's reCAPTCHA sitekey and action are located within the HTML code using the patterns data-sitekey='(.*?)' and action="(.*?)", respectively. If the website's HTML code uses different patterns you need to change the regex patterns accordingly.

If no matches are found, the code will print an error message and exit. Fix your regex patterns and try again.

NOTE: If you're not familiar with regular expressions or making requests in python, here are some resources that can help you get started 🤗:

  • Use this Regex Cheatsheet if you want some help with regular expressions.
  • Read the requests library documentation here.

Solving the reCaptcha Challenge

Now that you have the sitekey and action, you can send the reCaptcha challenge to 2Captcha and get the solution. Here's some sample code that shows you how to do that:

result = solver.recaptcha(
  sitekey=sitekey,
  url=url,
  version="3",
  action=action,
  score=0.6
)

print(result)
Enter fullscreen mode Exit fullscreen mode

NOTE: The code assumes that you're using reCaptcha V3, but you can also use it to solve reCaptcha V2 challenges. To do that, you'll need to pass only the sitekey and url.

Final Code

import re
import requests
import twocaptcha

url = 'https://www.example.com'
html = requests.get(url).text

# Set the regex patterns
sitekey_pattern = re.compile(r"data-sitekey='(.*?)'")
action_pattern = re.compile(r'action="(.*?)"')

# Search the HTML for the sitekey and action
sitekey_match = sitekey_pattern.search(html)
action_match = action_pattern.search(html)

# Check if the sitekey and action were found
if sitekey_match:
    sitekey = sitekey_match.group(1)
else:
    sitekey = None

if action_match:
    action = action_match.group(1)
else:
    action = None

# Error & exit if the sitekey or action were not found
if (sitekey == None) or (action == None):
    print("Error: Sitekey or Action not found ❌")
    print(f"Sitekey: {sitekey} | Action: {action}")
    exit()


result = solver.recaptcha(
  sitekey=sitekey,
  url=url,
  version="3",
  action=action,
  score=0.3
  )

print(result)

Enter fullscreen mode Exit fullscreen mode

Conclusion

And there you have it, folks! Now you know how to implement 2Captcha in your Python scripts. You can use this technique to automate any task that requires solving reCaptcha challenges.

Helpful Resources 📚

  1. 2Captcha Python Library
  2. 2Captcha API Documentation
  3. Google reCaptcha V3
  4. Selenium WebDriver
  5. Beautiful Soup library

Originally published on acidop.codes

Top comments (0)