How to bypass captcha using Python
Bypassing captcha is a common task in web scraping and automation. Captchas are designed to tell humans and bots apart, which makes data extraction harder. With our service API, you can solve captchas directly in Python. This step by step guide will walk you through the whole process.
Prerequisites
- Basic knowledge of Python.
- An active account with our service and an API key.
- Python 3.6 or newer installed.
- These libraries installed: seleniumbase, requests, chromedriver-autoinstaller-fix.
Step by Step Guide
Step 1. Install Required Libraries
The modules make API communication easier. Install them using pip.
bash
pip install seleniumbase requests chromedriver-autoinstaller-fix
Step 2. Get Your API Key
- Log in to your account on our service.
- Go to the API Key section.
- Copy your unique key and store it in a safe place.
Step 3. Set Up the Script with Error Handling
Here is a clean code example for solving Cloudflare Turnstile. All sensitive data is moved to variables.
python
import time
import chromedriver_autoinstaller_fix
import requests
from seleniumbase import Driver
from selenium.webdriver.common.by import By
chromedriver_autoinstaller_fix.install()
agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
driver = Driver(uc=True, headless=False, proxy=False, agent=agent)
# Replace with your target site
url = 'https://YOUR_TARGET_SITE.com'
driver.delete_all_cookies()
driver.uc_open_with_reconnect(url, reconnect_time=20)
time.sleep(5)
# Example of closing a banner. Adjust the selector for your site
# driver.find_element(By.CLASS_NAME, "cookie-close-btn").click()
time.sleep(2)
# Example of form input. Adjust for your site
# driver.find_element(By.ID, "email").send_keys("your_email@example.com")
# driver.find_element(By.ID, "password").send_keys("your_password")
time.sleep(3)
# Replace with the sitekey from your target site
sitekey = "YOUR_SITEKEY_HERE"
print("sitekey = ", sitekey)
user_agent = driver.get_user_agent()
my_key = "YOUR_2captcha.cn_KEY"
data = {"key": my_key,
"method": "turnstile",
"sitekey": sitekey,
"json": 1,
"pageurl": url,
"useragent": user_agent,
}
response = requests.post(f"https://2captcha.cn/in.php", data=data)
print("Sent to service", response.text)
s = response.json()["request"]
time.sleep(20)
while True:
solu = requests.get(f"https://2captcha.cn/res.php?key={my_key}&action=get&json=1&id={s}").json()
if solu["request"] == "CAPCHA_NOT_READY":
print(solu["request"])
time.sleep(10)
elif "ERROR" in solu["request"]:
print(solu["request"])
driver.close()
driver.quit()
exit(0)
else:
break
for key, value in solu.items():
print(key, ": ", value)
time.sleep(2)
solu = solu["request"]
driver.execute_script("document.getElementsByName('cf-turnstile-response')[0].value = arguments[0];", solu)
# Example of unlocking a button. Adjust the selector for your site
el = driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
driver.execute_script("arguments[0].removeAttribute('disabled')", el)
time.sleep(2)
print("Response inserted")
time.sleep(1)
time.sleep(20)
driver.close()
driver.quit()
Step 4. Run the Script
Save the code in a file, for example solve_captcha.py, and run it in your terminal.
bash
python solve_captcha.py
How the Script Works
- The browser starts with settings to avoid bot detection.
- Captcha parameters are sent to
https://2captcha.cn/in.php. - The script regularly checks
https://2captcha.cn/res.phpfor the status. - On success, the token is placed into the correct page field.
- On error, a clear message appears and the script closes safely.
Recommendations
- Store your API key in an environment variable, not in the code.
- Add short delays between requests to avoid account blocks.
- Watch your account balance to prevent unexpected interruptions.
- If you get a wrong answer, send a report through our service API. The funds will be refunded.
Error Code Reference
The table below shows common error codes from our service. Keep it handy for debugging.
| Error Code | Description |
|---|---|
| ERROR_NO_SLOT_AVAILABLE | Your bid is too low or the queue is full. Try again later or raise your bid. |
| ERROR_PAGEURL | The pageurl is missing or has a wrong format. Provide a correct page URL. |
| ERROR_CAPTCHA_UNSOLVABLE | The captcha could not be solved. Funds are automatically returned to your balance. |
| ERROR_TASK_ABSENT | The task parameter is missing in your createTask call. Check your request structure. |
| ERROR_BAD_PARAMETERS | Required parameters are missing or formatted wrong. Check the documentation. |
| ERROR_BAD_PROXY | Proxy settings are incorrect or the connection fails. Check the proxy:port:username:password format. |
Troubleshooting
- Authorization error: double check your API key in the account dashboard.
- Empty response: make sure the image or captcha parameters meet the service requirements.
- Timeouts: increase the wait time between status checks.
- IP block: add your current IP to the trusted list in your account settings.
- Proxy error: verify the proxy format and test the connection before sending a task.
Need Help?
If something does not work or you have setup questions, open a support ticket with our service. We will help you with integration, suggest the best settings, and answer all your questions.
Conclusion
By following this guide, you can add our service API to your Python projects for reliable captcha solving. This method works well for standard and advanced captchas in web scraping and automation tasks. You can easily adapt the code for different captcha types and websites. Set the parameters carefully for your specific case and always include error handling.