Python cookie保存为本地文件,二次利用

 madjar   2020-03-15 11:27   129 人阅读  0 条评论

There is no immediate way to do so, but it's not hard to do.

You can get a CookieJar object from the session as session.cookies, you can use pickle to store it to a file.

A full example:

import requests, pickle
session = requests.session()
# Make some calls
with open('somefile', 'wb') as f:
    pickle.dump(session.cookies, f)

Loading is then:

session = requests.session()  # or an existing session

with open('somefile', 'rb') as f:
    session.cookies.update(pickle.load(f))

The requests library has uses the requests.cookies.RequestsCookieJar() subclass, which explicitly supports pickling and a dict-like API, and you can use the RequestsCookieJar.update() method to update an existing session cookie jar with those loaded from a pickle file.


本文地址:https://www.lanol.cn/post/43.html
温馨提示:文章内容系作者个人观点,不代表Lan's Blog对观点赞同或支持。
版权声明:本文为转载文章,来源于 madjar ,版权归原作者所有,欢迎分享本文,转载请保留出处!

 发表评论


表情

还没有留言,还不快点抢沙发?