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)

posted @ 2024-10-11 16:42  __username  阅读(72)  评论(0编辑  收藏  举报

本文作者:DIVMonster

本文链接:https://www.cnblogs.com/guangzan/p/12886111.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。