当前位置:网站首页 > 更多 > 编程开发 > 正文

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

作者:CC下载站 日期:2020-03-15 00:00:00 浏览:55 分类:编程开发

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

You can get aCookieJarobject from the session assession.cookies, you can usepickleto store it to a file.

A full example:

importrequests,pickle
session=requests.session()
#Makesomecalls
withopen('somefile','wb')asf:
pickle.dump(session.cookies,f)

Loading is then:

session=requests.session()#oranexistingsession

withopen('somefile','rb')asf:
session.cookies.update(pickle.load(f))

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


您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯