把一个`Cookie`放在`CookieJar`中 [英] Putting a `Cookie` in a `CookieJar`

查看:124
本文介绍了把一个`Cookie`放在`CookieJar`中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用新的Python请求库来发出http请求。我从服务器获取一个cookie作为文本。如何将它变成一个 CookieJar 中的cookie?

I'm using the new Python Requests library to make http requests. I obtain a cookie from the server as text. How do I turn that into a CookieJar with the cookie in it?

推荐答案

我很困惑的这个问题。

I'm confused by this question. The requests library will put the cookies in the jar for you.

import requests
import cookielib


URL = '...whatever...'
jar = cookielib.CookieJar()
r = requests.get(URL, cookies=jar)
r = requests.get(URL, cookies=jar)

对URL的第一个请求将填充jar。第二个请求将cookie发送回服务器。标准库的urllib模块 cookielib 也是如此。

The first request to the URL will fill the jar. The second request will send the cookies back to the server. The same goes for the standard library's urllib module cookielib.

这篇关于把一个`Cookie`放在`CookieJar`中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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