如何自动获取 Cookie
本文尚处于草稿状态,内容可能不完整或存在错误
Cookie 有效期只有 3 小时,因此如果希望 12:30 自动抢票,建议在 12:00 获取 Cookie。
python.exe -m pip install --upgrade pip
pip install playwright
playwright installfrom playwright.sync_api import sync_playwright
USERNAME = "xxx"
PASSWORD = "xxx"
with sync_playwright() as p:
# 使用持久化浏览器
context = p.chromium.launch_persistent_context(
user_data_dir="./user_data", # 本地保存登录态
headless=False
)
page = context.new_page()
page.goto("https://ehall.szu.edu.cn/qljfwapp/sys/lwSzuCgyy/index.do")
page.locator('input[name="username"]').first.fill(USERNAME)
page.locator('input[name="passwordText"]').first.fill(PASSWORD)
page.locator('a[id="login_submit"]').first.click()
print("请手动完成短信验证码...")
# 等手动输入验证码 + 登录成功
page.wait_for_timeout(60000)
# 可选:验证是否真的登录成功
page.wait_for_load_state("networkidle")
# 获取 cookies
cookies = context.cookies()
cookie_header = "; ".join([
f"{c['name']}={c['value']}"
for c in cookies
if "ehall.szu.edu.cn" in c["domain"]
])
print(cookie_header)最后更新于