Clear cookies from Requests Python

前端 未结 1 362
伪装坚强ぢ
伪装坚强ぢ 2021-01-04 05:37

I created variable: s = requests.session()

how to clear all cookies in this variable?

相关标签:
1条回答
  • 2021-01-04 05:58

    The Session.cookies object implements the full mutable mapping interface, so you can call:

    s.cookies.clear()
    

    to clear all the cookies.

    Demo:

    >>> import requests
    >>> s = requests.session()
    >>> s.get('http://httpbin.org/cookies/set', params={'foo': 'bar'})
    <Response [200]>
    >>> s.cookies.keys()
    ['foo']
    >>> s.get('http://httpbin.org/cookies').json()
    {u'cookies': {u'foo': u'bar'}}
    >>> s.cookies.clear()
    >>> s.cookies.keys()
    []
    >>> s.get('http://httpbin.org/cookies').json()
    {u'cookies': {}}
    

    Easiest however, is just to create a new session:

    s = requests.session()
    
    0 讨论(0)
提交回复
热议问题