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 π₯:
- Captcha sent to them
(captcha -> 2Captcha)
- They send it to a worker
(2Captcha -> worker)
- A worker solves it
(worker -> 2Captcha)
- 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
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.
Install the 2Captcha Python library:
pip3 install 2captcha-python
- Create a new Python file and import the 2Captcha library:
import twocaptcha
api_key = 'YOUR_API_KEY_HERE'
solver = twocaptcha.TwoCaptcha(api_key)
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()
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)
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)
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 π
- 2Captcha Python Library
- 2Captcha API Documentation
- Google reCaptcha V3
- Selenium WebDriver
- Beautiful Soup library
Top comments (0)