playwright自动登录获取cookie/ck
有些登录逆向起来比较麻烦,就用了自动化
import time
import json
import random
from playwright.sync_api import Playwright, sync_playwright, expect
# https://xxxx.edu.cn/appportalweb/seatspace/
"""
需要提前10分钟获取ck
"""
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=True)
context = browser.new_context()
page = context.new_page()
page.goto("https://xxxx.edu.cn/appportalweb/seatspace/")
page.get_by_placeholder("职工号/学号/邮箱/别名").click()
time.sleep(random.uniform(.5, .6))
page.get_by_placeholder("职工号/学号/邮箱/别名").fill("20210xxxx")
page.get_by_role("textbox", name="密码").click()
time.sleep(random.uniform(.5, .6))
page.get_by_role("textbox", name="密码").fill("xxxx2xxx24")
time.sleep(random.uniform(.2, .3))
page.get_by_role("button", name="登 录").click()
cookies = context.cookies()
print(cookies)
# target_names = ['IDSTGC']
found_cookie_value = next((cookie['value'] for cookie in cookies if cookie['name'] == 'IDSTGC'), None)
print(found_cookie_value)
cookie_data = {'IDSTGC': found_cookie_value
# 添加时间
, 'time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
}
# 将数据写入 JSON 文件
with open('config.json', 'w') as json_file:
json.dump(cookie_data, json_file)
# ---------------------
time.sleep(random.uniform(.5, .6))
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
本文来自博客园,作者:__username,转载请注明原文链接:https://www.cnblogs.com/code3/p/18458793