如何将cookie作为字符串

时间:2015-09-14 19:45:03

标签: python cookies python-requests

我正在使用请求,并设置了一些Cookie:

r = session.post('https://www.url.com?locale=en-US', data=obj, headers=self.headers)
self.cookies = session.cookies
>>> Cookies: <<class 'requests.cookies.RequestsCookieJar'>[<Cookie URLCookies=true for .url.com/>, <Cookie URLId=v%3D2%26ct%3DBQAOAAEBENBvTYO9c_2NHZGl125w2cSBgPl8sP0P_4ObzxbnXeEM_u9L_ttOWiMtFpm1UL_gp0U4j4HLBFzMwZexjJT0AqBT85pJE1DgYQtVvWs0jfzSQ80BbR6tHOvZZFqjUjRjA7bk8w2TlLAwyIpdwgRWChjq2FQE1ULIqtFQmXlXUu5Ka9WTlIGbvYh9GcDSV18U6ZMUzzYgRbKKo6cKIXEyZRSm8FYJ_tsD5zEsuCwnyv6TUALDSI_ro3AB1eFFwvs_dPrxZbhJXgUaSpiYEwjixOXAkwxnQxl5Ml3oLlVq7U2GkOX-ksiDpbZlPbyN2eYBKiEserBEOqkH6bgKkVi20hDr_W4ltzWCe2cMKFeQAUrCguURhrwhN37ByNR4405atX-x9p-zOkOniXw0xu3YBnYO5Vx-w5WpziJ_XZFi_LOn0Fb7GDWONT5iPnFivwSFQQHF7luOw19-KbJlJCOx47X3cnv2qiv1UcjymZR3wgbzbWKSKY-7q2h6PsXd25tnzUoZ95PMn0k-bU6bI0WiU1U2cA..%26bt%3Dusr%26ch%3DAQEAEAABABReUU1z_ICZv1gJPJE8tTkAleFwpAtlkb0.%26mac%3DAQEAEAABABSWe3pyMiwdAdC3PyRpZkVn43UCcOrD000. for .url.com/>, <Cookie SecureURLId=v%3D2%26mac%3DAQEAEQABABRT4DD2w4BI1bKhsNGvUB46b76-9tbTYB8.%26dt%3D1442259697698 for .netflix.com/>, <Cookie docBytes=71605 for .url.com/>, <Cookie memclid=01d8b5f7-7013-4a9d-8923-f9eead9782bc for .url.com/>, <Cookie profilesNewSession=0 for .url.com/>, <Cookie profilesNewUser=0 for .url.com/>]>

我如何打印cookie值,以便将其作为标题插入?例如:

headers = {
    'Language': 'en-US',
    'Cookies': 'asoidf3493434...'
}

当我执行print str(cookies)时,它并没有给我任何有用的信息,可以在请求中提供标题。我该怎么做?

1 个答案:

答案 0 :(得分:1)

print dict(r.cookies) # show all cookies received. OR
print requests.utils.dict_from_cookiejar(r.cookies) # same as above

print r.cookies['x'] # to get cookie x