在 Python 中接受 Cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/525773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:17:46  来源:igfitidea点击:

Accept Cookies in Python

pythoncookies

提问by Tom Dunham

How can I accept cookies in a python script?

如何在 python 脚本中接受 cookie?

回答by Tom Dunham

Try this:

试试这个:

import urllib2 
import cookielib

jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))

print "Currently have %d cookies" % len(jar)
print "Getting page"
response = opener.open("http://google.com")
print response.headers
print "Got page"
print "Currently have %d cookies" % len(jar)
print jar

It should print

它应该打印

Currently have 0 cookies
...
Currently have 2 cookies

(Google always sets a cookie). You don't really need this much unless you want to save your cookies to disk and use them later. You should find that

(谷歌总是设置一个cookie)。除非您想将 cookie 保存到磁盘并在以后使用它们,否则您实际上并不需要这么多。你应该发现

urllib2.build_opener(HTTPCookieProcessor).open(url)

Takes care of most of what you want.

照顾您想要的大部分内容。

More info here:

更多信息在这里:

回答by Gal Bracha

The easiest way is to use requestslibrary.

最简单的方法是使用请求库。

import requests
url = 'http://www.google.com/doodles/'
r = requests.get(url)
print r.cookies

回答by Can Berk Güder

You might want to look at cookielib.

你可能想看看cookielib

回答by Anonymous

I believe you mean having a Python script that tries to speak HTTP. I suggest you to use a high-level library that handles cookies automatically. pycurl, mechanize, twill - you choose.

我相信你的意思是有一个试图说 HTTP 的 Python 脚本。我建议您使用自动处理 cookie 的高级库。pycurl,机械化,斜纹 - 您选择。

For Nikhil Chelliah:

对于 Nikhil Chelliah:

I don't see what's not clear here.

我看不出这里有什么不清楚的。

Acceptinga cookie happens client-side. The server can seta cookie.

接受cookie 发生在客户端。服务器可以设置cookie。

回答by Nikhil Chelliah

It's unclear whether you want a client-side or a server-side solution.

目前还不清楚您想要客户端还是服务器端解决方案。

For client-side, cookielibwill work fine. This answerand a few web tutorials offer more in-depth explanations.

对于客户端,cookielib可以正常工作。 这个答案和一些网络教程提供了更深入的解释。

If this is a server-side problem, you should be using a framework that takes care of all the boilerplate. I really like how CherryPyand web.pyhandle them, but the API is pretty simple in any library.

如果这是服务器端问题,您应该使用一个框架来处理所有样板文件。我真的很喜欢CherryPyweb.py处理它们的方式,但 API 在任何库中都非常简单。

回答by Vasil

There's the cookielib library. You can also implement your own cookie storage and policies, the cookies are found in the set-cookie header of the response (Set-Cookie: name=value), then you send the back to a server in one or more Cookie headers in the request (Cookie: name=value).

有 cookielib 库。您也可以实现自己的 cookie 存储和策略,cookie 位于响应的 set-cookie 标头中 (Set-Cookie: name=value),然后您在响应的一个或多个 Cookie 标头中将其发送回服务器请求(Cookie:名称=值)。