将“Cookie"放入“CookieJar" [英] Putting a `Cookie` in a `CookieJar`
问题描述
我正在使用新的 Python 请求库来发出 http 请求.我从服务器获取一个 cookie 作为文本.我如何将它变成带有 cookie 的 CookieJar
?
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?
推荐答案
我被这个问题搞糊涂了.requests 库会为您将 cookie 放入 jar 中.
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 也是如此.(文档目前可用于 2.x 版本)
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. (doc currently available for 2.x Version)
这篇关于将“Cookie"放入“CookieJar"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!