CAPTCHA Solving for Web Scraping: Proxies, 2captcha, and What Works
Introduction
CAPTCHA solving is a common challenge for developers working on web scraping and automation tasks. CAPTCHAs are designed to prevent automated bots from accessing websites, but for legitimate purposes like data extraction or monitoring services, bypassing these protections can be necessary. In this guide, we'll explore strategies for solving CAPTCHAs using Python, focusing on the 2Captcha API, reCAPTCHA v3 token injection, and the impact of residential proxies on CAPTCHA trigger rates.
Understanding CAPTCHAs
Before diving into solutions, it's essential to understand how CAPTCHAs work. There are several types of CAPTCHAs:
- reCAPTCHA: Google’s CAPTCHA solution that can be divided into reCAPTCHA v2 and reCAPTCHA v3.
- v2: Requires users to click a checkbox or solve a puzzle to verify they're human.
- v3: Invisible to users, it assesses the risk of a user’s behavior without requiring interaction.
- hCaptcha: An alternative to Google's CAPTCHA, designed to be more privacy-friendly.
- Image-based CAPTCHAs: Require users to identify objects in images or solve simple puzzles.
Solving CAPTCHAs with 2captcha API
2captcha is a popular service that provides APIs for solving various types of CAPTCHAs. It's widely used because it supports many different CAPTCHA formats and offers a reliable solution.
Setting Up 2captcha
- Sign Up: Create an account at 2captcha.com.
- Get API Key: Navigate to the dashboard and find your API key.
- Install Python Library: Use the
requestslibrary to interact with the 2captcha API.
pip install requests
Example Code: Solving reCAPTCHA v2
Here’s how you can use 2captcha to solve a reCAPTCHA v2:
import requests
def solve_captcha(site_key, page_url, api_key):
# Submit the CAPTCHA to 2captcha API
response = requests.post(
'https://2captcha.com/in.php',
data={
'key': api_key,
'method': 'userrecaptcha',
'googlekey': site_key,
'pageurl': page_url,
}
)
captcha_id = response.text.split('|')[1]
# Wait for the CAPTCHA to be solved
while True:
result = requests.get(
'https://2captcha.com/res.php',
params={
'key': api_key,
'action': 'get',
'id': captcha_id,
}
)
if result.text.startswith('OK'):
break
time.sleep(5) # Wait for 5 seconds before checking again
return result.text.split('|')[1]
# Example usage
site_key = 'YOUR_SITE_KEY'
page_url = 'YOUR_PAGE_URL'
api_key = 'YOUR_2CAPTCHA_API_KEY'
captcha_solution = solve_captcha(site_key, page_url, api_key)
print(f'Captcha solution: {captcha_solution}')
Cost Analysis of Solver APIs
2captcha offers a pricing plan based on the number of CAPTCHAs solved. For instance:
- Free Tier: 100 solves per month
- Basic Plan: $5 for 1,000 solves
- Pro Plan: $15 for 5,000 solves
When using solver APIs like 2captcha, consider the cost of solving CAPTCHAs in your scraping operations. The cost can add up quickly if you're dealing with a large volume of CAPTCHAs.
reCAPTCHA v3 Token Injection
reCAPTCHA v3 doesn’t require user interaction but instead evaluates the risk level of a user’s behavior. You can inject tokens into HTTP requests to bypass it, though this approach should be used responsibly and ethically.
Example Code: Injecting reCAPTCHA v3 Token
import requests
def get_recaptcha_token(api_key, site_key, action='homepage'):
# Send a request to Google's reCAPTCHA API to get the token
response = requests.post(
'https://www.google.com/recaptcha/api.js',
data={
'key': api_key,
'sitekey': site_key,
'action': action,
}
)
return response.json()['token']
# Example usage
api_key = 'YOUR_GOOGLE_RECAPTCHA_API_KEY'
site_key = 'YOUR_SITE_KEY'
recaptcha_token = get_recaptcha_token(api_key, site_key)
print(f'ReCaptcha token: {recaptcha_token}')
Note: This is a simplified example. In practice, you might need to handle additional parameters and use browser automation tools like Selenium or Playwright.
Impact of Residential Proxies
Residential proxies, such as those provided by Bright Data, can significantly affect how often CAPTCHAs are triggered. These proxies have IP addresses associated with real users, which can reduce the likelihood of triggering security measures.
Example Code: Using a Residential Proxy with Requests
import requests
def fetch_with_proxy(url, proxy):
response = requests.get(
url,
proxies={
'http': proxy,
'https': proxy,
}
)
return response.text
# Example usage
url = 'YOUR_TARGET_URL'
proxy = 'http://residential.proxy.brightdata.com:PORT'
page_content = fetch_with_proxy(url, proxy)
print(page_content)
Cost Considerations: Residential proxies can be more expensive than data center proxies but are often necessary for bypassing CAPTCHAs and other security measures.
Conclusion
Solving CAPTCHAs is a complex task that requires understanding the different types of CAPTCHAs and choosing the right tools and strategies. Using services like 2captcha can provide reliable solutions, but it's important to consider costs and ethical implications. Injecting reCAPTCHA v3 tokens can be an effective way to bypass certain security measures, though it should be used judiciously.
When managing proxies at scale, residential proxies can help reduce CAPTCHA trigger rates but come with higher costs. If you're looking for a simple solution to manage and test proxies locally, ProxyForge might be worth considering. It auto-tests and rotates proxies on your machine without requiring subscriptions:
https://proxyforge.dev/#download
Remember, always use these tools responsibly and ethically, respecting the terms of service of websites you interact with.
Free proxies break. ProxyForge doesn't.
The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.
Download ProxyForge free →