init commit
This commit is contained in:
28
util.py
Normal file
28
util.py
Normal file
@@ -0,0 +1,28 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
util.py - 用于保存和读取 cookie 的工具函数
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
|
||||
def save_cookies(context, save_path):
|
||||
"""
|
||||
保存 Playwright 浏览器上下文中的 cookies 到指定路径
|
||||
:param context: Playwright browser context
|
||||
:param save_path: 保存 cookie 的文件路径
|
||||
"""
|
||||
cookies = context.cookies()
|
||||
with open(save_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(cookies, f, ensure_ascii=False, indent=2)
|
||||
|
||||
def load_cookies(context, load_path):
|
||||
"""
|
||||
从指定路径读取 cookies 并设置到 Playwright 浏览器上下文
|
||||
:param context: Playwright browser context
|
||||
:param load_path: cookie 文件路径
|
||||
"""
|
||||
if not os.path.exists(load_path):
|
||||
return
|
||||
with open(load_path, 'r', encoding='utf-8') as f:
|
||||
cookies = json.load(f)
|
||||
context.add_cookies(cookies)
|
||||
Reference in New Issue
Block a user