python - 用requests进行爬虫,怎么获得有cookie的字典?
高洛峰
高洛峰 2017-04-18 09:17:52
0
2
440

比如:f12观察到
Cookie:JSESSIONID=qzGdVaHzBrI1cg+9X2iPaCPS.undefined

session=requests.session()
html1=session.get(url=url,headers=headers)
html2=session.get(url=urlIndex,headers=headers,cookies=cookies????)

怎么才能获取到这个字典呢{'Cookie':'JSESSIONID=qzGdVaHzBrI1cg+9X2iPaCPS.undefined'}?
我输出html.cookies.values/items/get/get_dict,得到的结果都是:

<bound method RequestsCookieJar.keys of <RequestsCookieJar[Cookie(version=0, name='JSESSIONID', value='KEUJT8xlQFKX+r6R-fq1lhnI.undefined', port=None, port_specified=False, domain='218.57.139.24', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]>>

这样的类型能怎么转化呢?还是cookies直接赋值,另cookies=html1.cookies就行了?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
黄舟

The session of requests will automatically update the cookies for you. Why bother to get them again? If you are really worried, you can capture the packet and take a look

迷茫

http://stackoverflow.com/ques...
From the documentation:

get cookie from response

url = 'http://example.com/some/cooki...'
r = requests.get(url)
r.cookies

{'example_cookie_name': 'example_cookie_value'}

give cookie back to server on subsequent request

url = 'http://httpbin.org/cookies'

cookies =dict(cookies_are='working')
r =requests.get(url,cookies=cookies)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template