import json import os import argparse import requests from playwright.sync_api import sync_playwright def save_login_state(auth_file, keyword): with sync_playwright() as p: os.makedirs(os.path.dirname(auth_file), exist_ok=True) # headless=False 表示显示浏览器窗口,方便用户扫码登录 browser = p.chromium.launch(headless=False) context = browser.new_context() page = context.new_page() login_success = False def on_response(response): nonlocal login_success if '/login_jump' in response.url: if response.status == 200: print(f"[网络监听] 检测到登录API响应成功") print(f" - 请求地址: {response.url}") print(f" - 状态码: {response.status}") login_success = True def on_url_change(frame): nonlocal login_success current_url = page.url if 'home/index?sid' in current_url: print(f"[URL监听] 检测到页面已跳转") print(f" - 当前URL: {current_url}") login_success = True # 其他可用的事件:'request'(请求发送时)、'requestfailed'(请求失败时)、'requestfinished'(请求完成时) page.on('response', on_response) page.on('framenavigated', on_url_change) page.goto('https://mail.qq.com') try: # 毫秒单位,300000ms = 300秒 = 5分钟 page.wait_for_function(f'document.body.innerText.includes("{keyword}")', timeout=300000) print(f"[文本监听] 检测到'{keyword}'文字,登录确认成功!") except Exception as e: print(f"[警告] 未检测到'{keyword}'文字,可能页面结构有变化或登录超时") print(f" 错误信息: {e}") if login_success: print("已确认登录成功!") else: print("\n⚠️ 未明确检测到登录成功标志,但仍将保存当前状态") print(" 如果登录成功,状态应该是有效的") page.wait_for_timeout(500000) # 2000毫秒 = 2秒 context.storage_state(path=auth_file) print(f"✅ 登录状态已保存到: {auth_file}") browser.close() print("浏览器已关闭") # 1. 从 Playwright 保存的 auth.json 中提取 cookies def extract_cookies_from_auth(auth_file): """ 从 Playwright 的 auth.json 提取 cookies Args: auth_file: auth.json 文件路径 Returns: dict: requests 可用的 cookies 字典 """ with open(auth_file, 'r', encoding='utf-8') as f: auth_data = json.load(f) cookies = {} for cookie in auth_data.get('cookies', []): # Playwright 保存的每个 cookie 包含 name 和 value cookie_name = cookie['name'] cookie_value = cookie['value'] cookies[cookie_name] = cookie_value return cookies # 2. 使用 requests 访问需要登录的页面 def fetch_with_requests(auth_file, url='https://mail.qq.com'): # 提取 cookies cookies = extract_cookies_from_auth(auth_file) print("提取到的 cookies:") for name, value in cookies.items(): print(f" {name}: {value[:20]}..." if len(value) > 20 else f" {name}: {value}") # 创建 session 并设置 cookies session = requests.Session() session.cookies.update(cookies) # 设置请求头(模拟浏览器) session.headers.update({ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'zh-CN,zh;q=0.9', }) # 发送请求 response = session.get(url) print(f"响应状态码: {response}") return response if __name__ == "__main__": parser = argparse.ArgumentParser(description='爬虫脚本') parser.add_argument('account', help='用户') args = parser.parse_args() account = args.account # account = "123" print(f"用户名{account}") if not account: print("请输入用户名") exit(1) auth_file_path = "./auth/mail" file_path = f"{auth_file_path}/{account}.json" if os.path.exists(file_path): fetch_with_requests(file_path) else: save_login_state(file_path,"收件箱")