Python中如何设置Cookie并跟随重定向请求?

There was problem when I tried to grab BAIDU tongji infor.

When I logined success, there was a 302 redirection to main page.

That means the internal redirect was from https://tongji.baidu.com/web/welcome/ico?s=sdfsdfsdfsdf to s://tongji.baidu.com/web/12323243/overview/index?siteId=sdfsf.

I wonder know that how does the program(may be the broswer? I am also not clear. LOL) pass the cookie from the 320 page to the destination page? and Why?

Could anyone do me a favor? Thanks in advace.

Append:

302 page : https://tongji.baidu.com/web/welcome/ico?s=sdfsdfsdfsdf

destination page: s://tongji.baidu.com/web/12323243/overview/index?siteId=sdfsf


Python中如何设置Cookie并跟随重定向请求?

13 回复

Chinese please or go to Stack Overflow


import requests

# 创建会话对象保持Cookie
session = requests.Session()

# 首次请求获取初始Cookie(如果需要)
initial_response = session.get('https://example.com/login')
# 通常登录后服务器会设置Cookie

# 设置自定义Cookie(两种方式)
# 方式1:通过字典设置(将随请求发送)
session.cookies.update({
    'session_id': 'abc123',
    'user_token': 'xyz789'
})

# 方式2:使用CookieJar对象
from http.cookies import SimpleCookie

cookie = SimpleCookie()
cookie['custom_cookie'] = 'value'
cookie['custom_cookie']['domain'] = 'example.com'
cookie['custom_cookie']['path'] = '/'

# 将Cookie添加到会话
session.cookies.update(cookie)

# 发送请求并自动跟随重定向(默认allow_redirects=True)
response = session.post(
    'https://example.com/protected',
    data={'key': 'value'},
    allow_redirects=True  # 这是默认值,会自动处理重定向
)

# 如果需要手动处理重定向流程:
response = session.post(
    'https://example.com/protected',
    data={'key': 'value'},
    allow_redirects=False
)

if response.status_code in (301, 302, 303, 307, 308):
    redirect_url = response.headers['Location']
    # 会话会自动携带Cookie访问重定向地址
    final_response = session.get(redirect_url, allow_redirects=True)

# 验证Cookie是否生效
print("当前会话Cookie:", session.cookies.get_dict())
print("最终响应状态码:", response.status_code)

核心要点:用requests.Session()管理会话,设置allow_redirects=True自动跟随重定向,Cookie会自动保持。

总结建议:用Session对象配合allow_redirects参数。

I am a little worried about whether this question lives up to the Stack Overflow’s strict standard.

不知道你看得懂中文不
中文答案:
1. http 是无状态的
2. cookie 是通过 header 传递的
3. 留意一下 cookie 的域

Because the cookie is shared by same domain

well, Is it that means the 302 page request cookie will also pass to the destination page by header and it also acts as a request cookie in the destination page?

logined 是什么

a past tense of word “login”, it means events or actions happen in the past.

如果你想理解 cookie 在 302 页面中的表现,就必须先了解 cookie 在普通页面中的表现。
如我刚才所说:
1. http 是无状态的
这个是前提。
cookie 存在本地,无状态的情况下,不关心你有没有做 302 跳转。

Thanks for your kindness and patience. There seems like a outline about this.

这么秀的吗?看历史原来你不是会中文么?

之前刚做了,用 session 啊,统计后台设置个第三方密码,然后 post 给他,保持 session 去请求其他页面,接着统计里的数据随便拿~ 那去参考下把;我是 py 新手;

<br>##百度统计的第三查看密码,登录并获得 session 和 siteid<br>idwd = {'passwd': '66666'}<br>S = Session() <br>logined = <a target="_blank" href="http://S.post" rel="nofollow noopener">S.post</a>("<a target="_blank" href="https://tongji.baidu.com/web/welcome/ico?s=8dfdafdafadfa4bccd" rel="nofollow noopener">https://tongji.baidu.com/web/welcome/ico?s=8dfdafdafadfa4bccd</a>", data=idwd, headers=REQ_HEADERS)<br><br>#获得 siteid,并转换成字符串<br>siteid= str(logined.url.split("=")[1])<br>webid = str(logined.url.split("/")[4])<br><br><br><br>##搜索词的 post 参数<br>keyjson = {"siteId":siteid,"st":"","et":"","st2":"","et2":"","indicators":"['pv_count','visitor_count','ip_count','bounce_ratio','avg_visit_time']","order":"pv_count,desc","offset":"","pageSize":"","target":"-1","flag":"indicator","source":"","isGroup":"0","clientDevice":"all","reportId":"12","method":"source/searchword/a","queryId":""}<br><br>readkeyjson = <a target="_blank" href="http://S.post" rel="nofollow noopener">S.post</a>("<a target="_blank" href="https://tongji.baidu.com/web/" rel="nofollow noopener">https://tongji.baidu.com/web/</a>"+webid+"/ajax/post", data=keyjson, headers=REQ_HEADERS)<br><br><br>#按文本读取<br>jsondata = readkeyjson.text<br>#格式化 json<br>readjsondict = json.loads(jsondata)<br><br>keyNamejson = readjsondict['data']['items'][0]<br>for items in keyNamejson:<br> items2 = items<br> print(items2[0]['name'])<br><br>

well, I do not know whether your method is workable. But thank you all the same.

回到顶部