suchen

Heim  >  Fragen und Antworten  >  Hauptteil

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 Tage vor449

Antworte allen(2)Ich werde antworten

  • 黄舟

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

    requests的session就自动给你更新了cookies啊,何必再去获取,实在不放心,你可以抓包看看

    Antwort
    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)

    Antwort
    0
  • StornierenAntwort