Python requests 如何获取第一次GET请求时 Request Headers 中的 Cookies?
巴扎黑
巴扎黑 2017-04-18 10:24:02
0
3
1420

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

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

请问有什么解决的方法?

巴扎黑
巴扎黑

reply all(3)
Peter_Zhu

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.

刘奇

Use requests.Session()

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

url = 'http://example.com'

resp = requests.get(url=url)
print 'resp', resp.cookies._cookies
print 'req',  resp.request._cookies._cookies
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template