如何在使用Python请求库的发布请求中发送Cookie

如何在使用Python请求库的发布请求中发送Cookie

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

问题描述

我尝试使用库发送带有post请求,但我不知道如何基于其文档实际设置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

但是,请求这是唯一的示例:

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

cookies = dict(cookies_are='working')

如何使用这个库来编码上面的cookie?

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?

推荐答案

我们需要使用python的标准cookie库来发送它,请求将从简单的字典为您构建CookieJars。

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

import requests

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

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

享受:)

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

07-25 10:27