在重试的功能上设置有效期



我正在收获验证码,验证码仅在2分钟内有好处。我正在尝试弄清楚如何设置"过期的,再次获取"函数

def captchaToken():
    global captoken
    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(APItoken, sitekey, url)).text.split('|')[1]
    #time.sleep(5)
    print("Waiting for captcha...")
    # then we parse gresponse from 2captcha response
    recaptcha_answer = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(APItoken, captcha_id)).text
    print("Solving captcha...")
    while 'CAPCHA_NOT_READY' in recaptcha_answer:
        print("Waiting for a response . . .")
        time.sleep(2.5)
        recaptcha_answer = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(APItoken, captcha_id)).text
    recaptcha_answer = recaptcha_answer.split('|')[1]
    captoken = recaptcha_answer
    print(Fore.MAGENTA + "Captcha Response: "+recaptcha_answer)

我建议:

while 1:
   captcha = get_captcha()
   request = post_captcha(captcha)
   start = time.time()
   while 1:
      response = poll_for_result(request)
      if start + 120 < time.time():
         time_is_up()
         break
      if response:
         return deal_with_response(response)

相关内容

  • 没有找到相关文章

最新更新