requests中读取和设置Cookie

读取和设置Cookie中也非常简单;requests返回的Response中包含一个cookies属性,访问它,将返回一个RequestsCookieJar对象。

import requests
response = requests.get('https://www.baidu.com')
response.cookies
<RequestsCookieJar[Cookie(version=0, name='BDORZ', value='27315', port=None, port_specified=False, domain='.baidu.com', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1593915673, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False)]>

你也可以以字典的方式读取具体的值。

设置Cookie

只需要将一个RequestsCookieJar对象传递给cookies参数即可:

jar = requests.cookies.RequestsCookieJar()
jar.set('cookie1', 'yum', domain='youdomain.com', path='/cookies')
jar.set('cookie2', 'blech', domain='youdomain.com', path='/elsewhere')
r = requests.get(url, cookies=jar)

原创内容,如需转载,请注明出处;

本文地址: https://www.perfcode.com/p/requests-cookie.html

分类: 计算机技术
推荐阅读:
最新阿里云免费SSL证书申请教程 2021年最新申请阿里云免费SSL证书流程如下:
warning: implicit declaration of function 'getpid' 解决方法 在C程序中使用getpid()获取进程识别码时,可能会出现 warning: implicit declaration of function 'getpid'; did you mean 'getenv'? [-Wimplicit-function-declaration] 这样的警告信息;
C语言islower()函数:判断字符是否为小写字母 islower()是C语言标准库中的一个函数,用于检查一个字符是否为小写字符;如果传入的字符参数是小写字母,则返回非0值,否则返回0;
Python将二维数组进行顺时针旋转90度、180度、270度 本文将使用Python实现二维数组顺时针旋转,包括旋转90度、180度、270度;
Python locals()函数 在 Python 中,locals() 是一个内置函数,用于返回当前作用域中的所有局部变量的字典。在函数内部,locals() 返回该函数的局部变量。在模块级别上,locals() 返回全局变量。
C语言获取当前系统的CPU核心数量 本文将介绍C语言在Windows系统和Linux系统下获取CPU核心数量的方法;