search

Home  >  Q&A  >  body text

python - 用requests进行爬虫,怎么获得有cookie的字典?

比如: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就行了?

高洛峰高洛峰2889 days ago447

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-18 09:19:52

    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

    reply
    0
  • 迷茫

    迷茫2017-04-18 09:19:52

    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)

    reply
    0
  • Cancelreply