我对 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)
请让我知道如何使用此代码解决图像验证码,以及我是否使用正确的工具来解决此验证码挑战。