从请求 Python 中清除 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23816139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:28:24 来源:igfitidea点击:
Clear cookies from Requests Python
提问by user3537765
I created variable: s = requests.session()
我创建了变量: s = requests.session()
how to clear all cookies in this variable?
如何清除此变量中的所有 cookie?
采纳答案by Martijn Pieters
The Session.cookies
object implements the full mutable mapping interface, so you can call:
该Session.cookies
对象实现了完整的可变映射接口,因此您可以调用:
s.cookies.clear()
to clear all the cookies.
清除所有 cookie。
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()