python请求获取cookie

时间:2014-08-02 05:27:52

标签: python python-requests

x = requests.post(url, data=data)
print x.cookies

我使用请求库从网站获取一些cookie,但我只能获取cookie 从Response,如何从请求中获取cookie?谢谢!

2 个答案:

答案 0 :(得分:68)

或者,您可以在请求之前和之后使用requests.Session并观察cookies

>>> import requests
>>> session = requests.Session()
>>> print(session.cookies.get_dict())
{}
>>> response = session.get('http://google.com')
>>> print(session.cookies.get_dict())
{'PREF': 'ID=5514c728c9215a9a:FF=0:TM=1406958091:LM=1406958091:S=KfAG0U9jYhrB0XNf', 'NID': '67=TVMYiq2wLMNvJi5SiaONeIQVNqxSc2RAwVrCnuYgTQYAHIZAGESHHPL0xsyM9EMpluLDQgaj3db_V37NjvshV-eoQdA8u43M8UwHMqZdL-S2gjho8j0-Fe1XuH5wYr9v'}

答案 1 :(得分:3)

如果每个Cookie都需要Users uid1 tokens token1: "value of token1" token2: "value of token2" path,而domain没有公开,则可以手动解析cookie,例如:

get_dict()