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

分类: 计算机技术
推荐阅读:
使用Python爬取网页上的所有链接 要使用Python爬取网页上的所有链接,可以使用Python的requests库和BeautifulSoup库。
Windows和Linux系统启用IP转发 在Windows系统下启用IP转发,需要通过修改注册表来实现;Linux系统只需将系统下的/proc/sys/net/ipv4/ip_forward文件值修改为1;
Go语言获取操作系统类型 在Go语言中,你可以使用runtime包来获取操作系统类型。具体来说,你可以使用runtime.GOOS来获取操作系统类型的字符串表示。
使用requests发送HTTP请求(GET和POST) 使用 Requests 发送HTTP请求非常简单;确保Python程序一开始导入了Requests模块:
rand()和srand()函数在C语言中的应用 本文通过示例介绍rand()函数和srand()函数在C语言中的用法;
Matlab中如何表示无穷大 在MATLAB中,您可以使用关键字inf(不区分大小写)来表示正无穷大,-inf表示负无穷大。