You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.2 KiB
34 lines
1.2 KiB
# coding=utf-8
|
|
|
|
import urllib.parse
|
|
import os
|
|
|
|
|
|
class CookieUtils:
|
|
@staticmethod
|
|
def cookie_str_to_dict(cookie_string):
|
|
cookie_dict = {}
|
|
# 分割字符串,每个cookie之间用分号隔开
|
|
cookie_pairs = cookie_string.split(';')
|
|
for pair in cookie_pairs:
|
|
# 去除前后空白字符
|
|
pair = pair.strip()
|
|
if pair: # 确保不处理空字符串
|
|
# 分割键和值,键和值之间用等号连接
|
|
key, value = pair.split('=', 1)
|
|
# 去除键和值的前后空白字符
|
|
key = key.strip()
|
|
value = value.strip()
|
|
# URL解码值(如果需要)
|
|
value = urllib.parse.unquote(value)
|
|
# 将键值对添加到字典中
|
|
cookie_dict[key] = value
|
|
return cookie_dict
|
|
|
|
@staticmethod
|
|
def read_cookie_dict_from_file(path='./cookie'):
|
|
if not os.path.exists(path):
|
|
raise RuntimeError(f"找不到cookie文件: {path}")
|
|
with open(path, 'r', encoding='utf-8') as file:
|
|
cookie_str = file.read()
|
|
return CookieUtils.cookie_str_to_dict(cookie_str)
|