CAPTCHAFORUM
Administrator
Picture captchas, also known as image captchas, are widely used on websites to prevent automated bots from performing unwanted actions. While they add a layer of security, solving them programmatically has become essential for legitimate use cases, such as web scraping or automated testing. In this article, we’ll discuss an effective way to bypass picture captchas using the 2Captcha service. We'll also provide examples of how to integrate it into your project.
Why Use 2Captcha?
2Captcha is a reliable and affordable service for solving captchas of all kinds, including image captchas. Its key features include:- High Accuracy: Real humans solve the captchas, ensuring high success rates.
- API Support: Simple and easy-to-use API for seamless integration.
- Low Cost: Competitive pricing for solving captchas at scale.
- Versatility: Supports various captcha types, including reCAPTCHA, hCaptcha, and custom image captchas.
How It Works
2Captcha uses a crowd-sourcing model where real people solve captchas on your behalf. The workflow is straightforward:- You upload the captcha image to 2Captcha via their API.
- A human worker solves the captcha.
- You receive the solution as a response.
Example Code
Here’s how to use the 2Captcha API to solve image captchas. Make sure you have an API key from 2Captcha before proceeding.Python Example
Code:
import requests
import time
# Your 2Captcha API key
API_KEY = 'your_2captcha_api_key'
# Step 1: Upload captcha image
url = 'http://2captcha.com/in.php'
files = {'file': open('captcha_image.jpg', 'rb')}
data = {'key': API_KEY, 'method': 'post'}
response = requests.post(url, files=files, data=data)
if response.text.startswith('OK'):
captcha_id = response.text.split('|')[1]
print(f"Captcha ID: {captcha_id}")
# Step 2: Wait for solution
result_url = f"http://2captcha.com/res.php?key={API_KEY}&action=get&id={captcha_id}"
for _ in range(30): # Try for 30 seconds
time.sleep(5)
result = requests.get(result_url).text
if result.startswith('OK'):
captcha_solution = result.split('|')[1]
print(f"Captcha solution: {captcha_solution}")
break
elif result == 'CAPCHA_NOT_READY':
print("Captcha not ready, retrying...")
else:
print(f"Error: {result}")
break
else:
print(f"Error: {response.text}")
Node.js Example
Code:
const axios = require('axios');
const fs = require('fs');
const API_KEY = 'your_2captcha_api_key';
async function solveCaptcha() {
try {
// Step 1: Upload captcha image
const formData = new FormData();
formData.append('file', fs.createReadStream('captcha_image.jpg'));
formData.append('key', API_KEY);
formData.append('method', 'post');
const uploadResponse = await axios.post('http://2captcha.com/in.php', formData, {
headers: formData.getHeaders(),
});
if (uploadResponse.data.startsWith('OK')) {
const captchaId = uploadResponse.data.split('|')[1];
console.log(`Captcha ID: ${captchaId}`);
// Step 2: Wait for solution
const resultUrl = `http://2captcha.com/res.php?key=${API_KEY}&action=get&id=${captchaId}`;
for (let i = 0; i < 30; i++) {
await new Promise((resolve) => setTimeout(resolve, 5000));
const resultResponse = await axios.get(resultUrl);
if (resultResponse.data.startsWith('OK')) {
const captchaSolution = resultResponse.data.split('|')[1];
console.log(`Captcha solution: ${captchaSolution}`);
return;
} else if (resultResponse.data === 'CAPCHA_NOT_READY') {
console.log('Captcha not ready, retrying...');
} else {
console.error(`Error: ${resultResponse.data}`);
return;
}
}
} else {
console.error(`Error: ${uploadResponse.data}`);
}
} catch (error) {
console.error(`Error: ${error.message}`);
}
}
solveCaptcha();
Best Practices
- Respect Website Policies: Ensure you have permission to use automated solutions on a website.
- Error Handling: Implement robust error handling to manage issues like invalid API keys or timeouts.
- Optimize Requests: Avoid excessive requests to reduce costs and improve efficiency.