Python请求 - 保存cookie以供以后使用url

时间:2021-08-02 09:00:33

I been trying to get a cookie and post it to a url in later use in the program, but I cant seem to get the cookie parameters to work.

我一直在尝试获取一个cookie并将其发布到以后在程序中使用的URL中,但我似乎无法使cookie参数起作用。

Right now I have

现在我有

response = requests.get("url")

But how exactly do I retrive cookies from this url and post them to a new url (the same cookies). The tutorial in requests is somewhat vague on the topic and gives examples I cannot test. Hope someone can help with further examples.

但是,我究竟如何从此网址中检索Cookie并将其发布到新网址(相同的Cookie)。请求中的教程在这个主题上有些模糊,并提供了我无法测试的示例。希望有人可以帮助进一步的例子。

This is python 2.7 btw.

这是python 2.7 btw。

1 个解决方案

#1


15  

You want to use a session:

您想要使用会话:

s = requests.session()

response = s.get('url')

You use the session just like the requests module (it has the same methods), but it'll retain cookies for you and send them along on future requests.

您可以像请求模块一样使用会话(它具有相同的方法),但它将为您保留cookie并在将来的请求中发送它们。

#1


15  

You want to use a session:

您想要使用会话:

s = requests.session()

response = s.get('url')

You use the session just like the requests module (it has the same methods), but it'll retain cookies for you and send them along on future requests.

您可以像请求模块一样使用会话(它具有相同的方法),但它将为您保留cookie并在将来的请求中发送它们。