[英]Using 2captcha with Python
我对 2captchas 和 Python 完全陌生,所以我想弄清楚这两个是如何工作的。 现在我正在编写一个 python 脚本并在 spyder 上运行它来解析图像验证码。 我的代码(使用 2captcha API)在返回响应中返回站点的 html。 它尝试注册一个站点,作为回报,它无法完成解决验证码的主要任务。 我的代码看起来像这样
import requests
from time import sleep
API_KEY = '2captchaapi' # Your 2captcha API KEY
site_key = '2captcha site key' # site-key, read the 2captcha docs on how to get this
url = 'site' # example url
proxy = 'proxy' # example proxy
proxy = {'http': 'http://' + proxy, 'https': 'https://' + proxy}
s = requests.Session()
# here we post site key to 2captcha to get captcha ID (and we parse it here too)
captcha_id = s.post(
"http://2captcha.com/in.php?key={}&method=userrecaptcha&googlekey={}&pageurl={}".format(API_KEY, site_key, url), proxies=proxy).text.split('|')[1]
# then we parse gresponse from 2captcha response
recaptcha_answer = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(API_KEY, captcha_id), proxies=proxy).text
print("solving ref captcha...")
while 'CAPCHA_NOT_READY' in recaptcha_answer:
sleep(5)
recaptcha_answer = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(API_KEY, captcha_id), proxies=proxy).text
recaptcha_answer = recaptcha_answer.split('|')[1]
print(recaptcha_answer)
payload = {
'signup-form[votes]': '',
'signin-form[subs]': '',
'signin-form[post_referer]': 'site',
'signup-form[step2][hidden_captcha]': '',
'signup-form[details][login]': 'name@gmail.com',
'signup-form[details][profile_name]': 'name1',
'signup-form[details][password]': 'secret44%',
'signup-form[details][password_confirm]': 'secret44%',
'signup-form[details][tos_pp]': 'on',
'signup-form[step2][optional][age]': '24',
'signup-form[step2][optional][sex]': 'Man',
'signup-form[step2][optional][country]': 'france',
'signup-form[step2][optional][language]': 'french',
'signup-form[step2][profilepic][file]': '',
'g-recaptcha-response': recaptcha_answer
}
# then send the post request to the url
response = s.post(url, payload, verify=False)
print(response.text)
请让我知道如何使用此代码解决图像验证码,以及我是否使用正确的工具来解决此验证码挑战。
您可以使用这个 python 库 - 2captcha-python 。 您想从图像验证码中提取文本,以便在安装和配置步骤后使用以下代码。
要绕过普通验证码(图像上的扭曲文本),请使用以下方法。 此方法还可用于识别图像上的任何文本。
result = solver.normal('path/to/captcha.jpg', param1=..., ...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.