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

Lan
Lan
2020-03-15 / 0 评论 / 614 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2020年03月15日,已超过1742天没有更新,若内容或图片失效,请留言反馈。

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.


0

评论 (0)

取消