diff --git a/spider/task.py b/spider/task.py index 4024bf8..1cb2a08 100644 --- a/spider/task.py +++ b/spider/task.py @@ -877,36 +877,30 @@ def login_with_2fa_retry(page, code_2fa, max_retries=3): ) code_input.fill(auth_code) - # 等待可能的验证结果 - page.wait_for_timeout(2000) # 等待2秒让系统处理 + # 点击继续 + page.query_selector('div[role="button"][aria-label="Continue"]').click() # 检查是否出现错误信息 - error_element = page.wait_for_selector( - 'span:has-text("This code doesn’t work. Check it’s correct or try a new one.")', timeout=10000) + try: + error_element = page.wait_for_selector( + 'span:has-text("This code doesn’t work. Check it’s correct or try a new one.")', timeout=3000) + if error_element and error_element.is_visible(): + print("2FA验证码错误,准备重试...") + retry_count += 1 - if error_element and error_element.is_visible(): - print("2FA验证码错误,准备重试...") - retry_count += 1 + # 清空输入框重新输入 + code_input.fill("") - # 清空输入框重新输入 - code_input.fill("") - - # 如果不是最后一次重试,等待一段时间再重试 - if retry_count < max_retries: - wait_time = 5 # 等待5秒 - print(f"等待 {wait_time} 秒后重试...") - time.sleep(wait_time) - continue - else: + # 如果不是最后一次重试,等待一段时间再重试 + if retry_count < max_retries: + wait_time = 5 # 等待5秒 + print(f"等待 {wait_time} 秒后重试...") + time.sleep(wait_time) + continue + except TimeoutError: logger.info("2FA验证成功!") return True - except TimeoutError: - logger.info("查找输入框超时,可能是页面加载问题") - retry_count += 1 - if retry_count < max_retries: - time.sleep(3) - continue except Exception as e: logger.info(f"2FA验证发生未知错误: {e}") retry_count += 1 @@ -1009,8 +1003,6 @@ def playwright_m_login(username, password, code_2fa=None): except Exception as e: logger.error(f"2FA验证失败: {e}", exc_info=True) raise OperationFailed(f"2FA验证失败: {e}") - # 点击继续 - page.query_selector('div[role="button"][aria-label="Continue"]').click() # 等待登录成功, 账号被检测自动化程序会卡在这里. 直接忽略报错 try: @@ -1145,7 +1137,7 @@ if __name__ == '__main__': # # # 视频链接的帖子点赞 # print(playwright_like(cookies, "https://www.facebook.com/groups/1070754870427928/permalink/1873461830157224/")) # - cookies = playwright_m_login('61582494086321', '47a67lDCC8', '4AQBIGCZDKKZ4WKKDZQIOAAKJ2A4QACR') + cookies = playwright_m_login('61583069571290', '47lfsryRmI', 'IJ6YM5MX6HEMRRIDSNUMSCEBJEIHVH3R') print(cookies) # pass