使用请求模块在标头中获取Set-Cookie [英] Get Set-Cookie in header using requests module

查看:167
本文介绍了使用请求模块在标头中获取Set-Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用PHP获取cookie时,它返回:

When I use PHP to get cookie, it returns:

session_id=abih14s7l4lgo3splta7f6bd14; cccaa78fa9e13785130119a4924db0f4=96637ae... (more)

但是当我使用Python时,它返回:

But when I use Python, it returns:

session_id=abih14s7l4lgo3splta7f6bd14

...其余的cookie丢失了。

... the rest of cookies is lost.

我的代码Python:

My code Python:

res_post = requests.post(LOGIN_URL, data = {mydata})
cookies = dict(res_post.cookies.items())


推荐答案

好像您正在使用请求库。 response.cookies 已经是一个字典,因此无需再次投射。

Looks like you're using the requests library. The response.cookies is already a dictionary, so there's no need to cast it again.

您应该访问Cookie上的响应如下:

You should access the cookies on the response like so:

response = requests.post(LOGIN_URL, data={mydata})
print response.cookies['session_id']

有关更多详细信息,请参见此处: http://docs.python-requests.org/en/latest/user/quickstart/#cookies

See here for more detail: http://docs.python-requests.org/en/latest/user/quickstart/#cookies

这篇关于使用请求模块在标头中获取Set-Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆