如何使用 Python 请求库在发布请求中发送 cookie? [英] How to send cookies in a post request with the Python Requests library?

查看:45
本文介绍了如何使用 Python 请求库在发布请求中发送 cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Requests 库发送带有发布请求,但我不确定如何根据其文档实际设置 cookie.该脚本用于维基百科,需要发送的 cookie 格式如下:

I'm trying to use the Requests library to send cookies with a post request, but I'm not sure how to actually set up the cookies based on its documentation. The script is for use on Wikipedia, and the cookie(s) that need to be sent are of this form:

enwiki_session=17ab96bd8ffbe8ca58a78657a918558e; path=/; domain=.wikipedia.com; HttpOnly

但是,requests 文档快速入门仅将此作为唯一示例:

However, the requests documentation quickstart gives this as the only example:

cookies = dict(cookies_are='working')

如何使用此库对上述 cookie 进行编码?我是否需要使用python的标准cookie库制作它,然后将其与POST请求一起发送?

How can I encode a cookie like the above using this library? Do I need to make it with python's standard cookie library, then send it along with the POST request?

推荐答案

最新版本的 Requests 将从简单的字典中为您构建 CookieJars.

The latest release of Requests will build CookieJars for you from simple dictionaries.

import requests

cookies = {'enwiki_session': '17ab96bd8ffbe8ca58a78657a918558'}

r = requests.post('http://wikipedia.org', cookies=cookies)

享受:)

这篇关于如何使用 Python 请求库在发布请求中发送 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆