From 907692c5e84f60e97827c9cd87a1993eb2307bab Mon Sep 17 00:00:00 2001 From: zayac Date: Sat, 6 Apr 2024 12:07:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5=E9=87=8D=E8=AF=95=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/login.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/core/login.py b/src/core/login.py index 87e9030..323ed98 100644 --- a/src/core/login.py +++ b/src/core/login.py @@ -39,11 +39,11 @@ def perform_login(playwright, account: Account) -> Account: page = context.new_page() page.goto(account.url) fill_login_form(page, account) - if handle_captcha(page,account.type): + if handle_captcha(page, account.type, retry_count=0): account.headers = capture_request_headers(page, account) logger.info('登录成功') else: - logger.error('登录失败或验证码处理失败') + logger.error('登录失败了') close_resources(page, context, browser) return account @@ -59,21 +59,24 @@ def fill_login_form(page, account: Account): logger.info(f'{account.name}登录ing...........') -def handle_captcha(page, account_type) -> bool: - try: - validate_code = page.locator('.geetest_box') - validate_code.wait_for(state='visible') - time.sleep(1) - return process_validate_code(page,validate_code,account_type) - except TimeoutError: - logger.error('超时了') - return False +def handle_captcha(page, account_type, retry_count) -> bool: + if retry_count < 3: + try: + validate_code = page.locator('.geetest_box') + validate_code.wait_for(state='visible') + time.sleep(1) + return process_validate_code(page, validate_code, account_type) + except TimeoutError: + retry_count += 1 + logger.error(f'验证码识别失败,正在重试:{retry_count}次') + return handle_captcha(page, account_type, retry_count) + return False -def process_validate_code(page,validate_code, account_type): +def process_validate_code(page, validate_code, account_type): validate_code_buffer = validate_code.screenshot() img = base64.b64encode(validate_code_buffer).decode('utf-8') - #九游滑动验证 需要单独处理 + # 九游滑动验证 需要单独处理 if account_type == AccountType.jy: res = base64_api(img=img, typeid=33) drag_btn = validate_code.locator(".geetest_btn") @@ -99,6 +102,9 @@ def process_validate_code(page,validate_code, account_type): except ValueError: logger.error(f"获取移动距离失败,实际返回内容为:{res}") return False + except TimeoutError: + logger.debug("验证码滑动失败") + return process_validate_code(page, validate_code, account_type) else: res = base64_api(img=img) if '|' in res: @@ -157,10 +163,9 @@ def persistence(account: Account, headers: dict): session.commit() logger.info(f'Headers persisted for account {account.name}') - # if __name__ == '__main__': # with open('C:\\Users\\Administrator\\Desktop\\Snipaste_2024-03-26_21-46-35.png', 'rb') as f: # base64_data = base64.b64encode(f.read()) # b64 = base64_data.decode('utf-8') # res = base64_api(img= b64,typeid=33) -# print(res) \ No newline at end of file +# print(res)