Home  >  Q&A  >  body text

Python requests 如何获取第一次GET请求时 Request Headers 中的 Cookies?

如何获取第一次访问网站时 Request Headers 中的 Cookies?
比如访问 http://g.jandan.net/signin 在Fiddler 下抓包查看:

requests 中 session.cookies 只能获得 Response Headers 中的 Cookies,无法获得 Request Headers 中的 Cookies。

请问有什么解决的方法?

巴扎黑巴扎黑2741 days ago1360

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 10:26:02

    There is a cookielib package in python that can obtain cookies. The first visit is without cookies~~
    The following is a test using firebug:
    Since I have visited it before, there are cookies for this website on the browser, so clear them.

    Visiting again at this time is the real first visit. If you visit again at this time, the packet capture is as follows:

    The website will return set-cookie in the request for the first visit. The browser will store the set-cookie, and the cookie value will be brought with it when you visit it in the future. So~ There is no cookie in the request header for the first visit. If yours is, it means you have visited before, and the cookie value is always retained in the browser.

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:26:02

    Use requests.Session()

    import requests
    s= requests.Session()#自动处理cookie
    s.get(url)
    

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:26:02

    import requests
    
    url = 'http://example.com'
    
    resp = requests.get(url=url)
    print 'resp', resp.cookies._cookies
    print 'req',  resp.request._cookies._cookies
    

    reply
    0
  • Cancelreply