编程语言
首页 > 编程语言> > Python 格式化 Cookie

Python 格式化 Cookie

作者:互联网

"""
将 各种格式的 cookie 转为 字典格式
"""


def cookie_factory(ck_str=None):
    cookie_dict = {}

    # 从浏览器 抓包 复制过来的 cookie 字符串
    if ck_str:
        for cookie in ck_str.split(';'):
            try:
                k, v = cookie.strip().split('=')
                cookie_dict[k] = v
            except Exception as e:
                print(f'异常格式的cookie:{cookie} \t 异常信息:{e}')

        print(cookie_dict)
        return


if __name__ == '__main__':
    # 从浏览器 抓包 复制过来的 cookie 字符串
    cookie_str = "max-age=31536000; expires=Sun, 26-Feb-23 09:35:18 GMT; domain=.baidu.com; path=/; version=1; comment=bd"
    cookie_factory(ck_str=cookie_str)

标签:ck,__,格式化,Python,Cookie,str,格式,cookie,dict
来源: https://www.cnblogs.com/jiyu-hlzy/p/15944068.html