How to save requests (python) cookies to a file?

前端 未结 10 1683
渐次进展
渐次进展 2020-11-30 17:35

How to use the library requests (in python) after a request

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
bot = requests.session         


        
相关标签:
10条回答
  • 2020-11-30 17:41

    Expanding on @miracle2k's answer, requests Sessions are documented to work with any cookielib CookieJar. The LWPCookieJar (and MozillaCookieJar) can save and load their cookies to and from a file. Here is a complete code snippet which will save and load cookies for a requests session. The ignore_discard parameter is used to work with httpbin for the test, but you may not want to include it your in real code.

    import os
    from cookielib import LWPCookieJar
    
    import requests
    
    
    s = requests.Session()
    s.cookies = LWPCookieJar('cookiejar')
    if not os.path.exists('cookiejar'):
        # Create a new cookies file and set our Session's cookies
        print('setting cookies')
        s.cookies.save()
        r = s.get('http://httpbin.org/cookies/set?k1=v1&k2=v2')
    else:
        # Load saved cookies from the file and use them in a request
        print('loading saved cookies')
        s.cookies.load(ignore_discard=True)
        r = s.get('http://httpbin.org/cookies')
    print(r.text)
    # Save the session's cookies back to the file
    s.cookies.save(ignore_discard=True)
    
    0 讨论(0)
  • 2020-11-30 17:42

    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)
  • 2020-11-30 17:43

    dtheodor's answer got 95% there, except this:

    session = requests.session(cookies=cookies)
    

    For me this raises an exception saying session() does not takes arguments.

    I worked around it by taking the keys/values on the cookie.get_dict and adding them manually to the session using:

    session.cookies.set(cookies.keys()[n],cookies.values()[n])
    
    0 讨论(0)
  • 2020-11-30 17:45

    This will do the job:

    session.cookies = LWPCookieJar('cookies.txt')
    

    The CookieJar API requires you to call load() and save() manually though. If you do not care about the cookies.txt format, I have a ShelvedCookieJar implementation that will persist on change.

    0 讨论(0)
  • 2020-11-30 17:51

    code for python 3

    Note that the great majority of cookies on the Internet are Netscape cookies. so if you want to save cookies to disk in the Mozilla cookies.txt file format (which is also used by the Lynx and Netscape browsers) use MozillaCookieJar

    from http.cookiejar import MozillaCookieJar
    import requests
    
    s = requests.Session()
    s.cookies = MozillaCookieJar('cookies.txt')
    # or s.cookies = MozillaCookieJar() and later use s.cookies.filename = 'cookies.txt' or pass the file name to save method.
    
    response = s.get('https://www.msn.com')
    
    s.cookies.save()
    

    the file is overwritten if it already exists, thus wiping all the cookies it contains. Saved cookies can be restored later using the load() or revert() methods.

    Note that the save() method won’t save session cookies anyway, unless you ask otherwise by passing a true ignore_discard argument.

    s.cookies.save(ignore_discard=True)
    

    using load method:

    load cookies from a file.

    Old cookies are kept unless overwritten by newly loaded ones.

    s.cookies.load()
    

    using revert method:

    Clear all cookies and reload cookies from a saved file.

    s.cookies.revert()
    

    you may need also to pass a true ignore_discard argument in load or revert methods.

    note about using MozillaCookieJar :

    Note This loses information about RFC 2965 cookies, and also about newer or non-standard cookie-attributes such as port.

    more reading

    0 讨论(0)
  • 2020-11-30 17:53

    I found that the other answers had problems:

    • They didn't apply to sessions.
    • They didn't save and load properly. Only the cookie name and value was saved, the expiry date, domain name, etc. was all lost.

    This answer fixes these two issues:

    import requests.cookies
    
    def save_cookies(session, filename):
        if not os.path.isdir(os.path.dirname(filename)):
            return False
        with open(filename, 'w') as f:
            f.truncate()
            pickle.dump(session.cookies._cookies, f)
    
    
    def load_cookies(session, filename):
        if not os.path.isfile(filename):
            return False
    
        with open(filename) as f:
            cookies = pickle.load(f)
            if cookies:
                jar = requests.cookies.RequestsCookieJar()
                jar._cookies = cookies
                session.cookies = jar
            else:
                return False
    

    Then just call save_cookies(session, filename) to save or load_cookies(session, filename) to load. Simple as that.

    0 讨论(0)
提交回复
热议问题