This commit is contained in:
mshe 2026-06-08 17:27:51 +08:00
parent 8f279fb2e4
commit 9ee583afcb
1 changed files with 8 additions and 8 deletions

View File

@ -9,11 +9,11 @@ import logging
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
# 日志会输出到 stderr不会污染 stdout # 日志会输出到 stderr不会污染 stdout
logging.info("服务器启动") logging.info("服务器启动")
def save_login_state(auth_file, keyword, display=0): def save_login_state(auth_file, keyword):
with sync_playwright() as p: with sync_playwright() as p:
os.makedirs(os.path.dirname(auth_file), exist_ok=True) os.makedirs(os.path.dirname(auth_file), exist_ok=True)
# headless=False 表示显示浏览器窗口,方便用户扫码登录 # headless=False 表示显示浏览器窗口,方便用户扫码登录
browser = p.chromium.launch(headless=False,env={"DISPLAY": display}) browser = p.chromium.launch(headless=False)
context = browser.new_context() context = browser.new_context()
page = context.new_page() page = context.new_page()
login_success = False login_success = False
@ -86,11 +86,11 @@ def extract_cookies_from_auth(auth_file):
return cookies return cookies
def crawl_with_saved_state(auth_file,display=0): def crawl_with_saved_state(auth_file):
with sync_playwright() as p: with sync_playwright() as p:
# 加载之前保存的登录状态 # 加载之前保存的登录状态
logging.info(f"加载认证文件: {auth_file}") logging.info(f"加载认证文件: {auth_file}")
browser = p.chromium.launch(headless=False,env={"DISPLAY": display}) # 可以无头模式了 browser = p.chromium.launch(headless=False) # 可以无头模式了
context = browser.new_context(storage_state=auth_file) context = browser.new_context(storage_state=auth_file)
page = context.new_page() page = context.new_page()
page.goto('https://mail.qq.com') page.goto('https://mail.qq.com')
@ -111,7 +111,7 @@ def crawl_with_saved_state(auth_file,display=0):
browser.close() browser.close()
def start(account, display=0): def start(account):
logging.info(f"用户名{account}") logging.info(f"用户名{account}")
if not account: if not account:
logging.error("请输入用户名") logging.error("请输入用户名")
@ -124,13 +124,13 @@ def start(account, display=0):
file_age_minutes = (time.time() - os.path.getmtime(file_path)) / 60 file_age_minutes = (time.time() - os.path.getmtime(file_path)) / 60
if file_age_minutes > 30: if file_age_minutes > 30:
logging.warning("认证文件已过期,请重新登录") logging.warning("认证文件已过期,请重新登录")
save_login_state(file_path, "收件箱", display) save_login_state(file_path, "收件箱")
else: else:
logging.info("检测到认证文件,尝试使用保存的登录状态进行爬取") logging.info("检测到认证文件,尝试使用保存的登录状态进行爬取")
crawl_with_saved_state(file_path,display) crawl_with_saved_state(file_path)
else: else:
logging.info("未检测到认证文件,启动登录流程") logging.info("未检测到认证文件,启动登录流程")
save_login_state(file_path, "收件箱",display) save_login_state(file_path, "收件箱")
if __name__ == '__main__': if __name__ == '__main__':
start("123", display=0) start("123", display=0)