如何在 Python 请求中使用 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31554771/
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-08-19 10:13:20  来源:igfitidea点击:

How to use cookies in Python Requests

pythoncookiespython-requests

提问by user1474157

I am trying to login to a page and access another link in the page.

我正在尝试登录一个页面并访问该页面中的另一个链接。

payload={'username'=<username>,'password'=<password>}
with session() as s:
    r = c.post(<URL>, data=payload)
    print r
    print r.content

This is giving me a "405 Not Allowed" error. I checked the post method details using chrome developer tools and could see an api (URL/api/auth). I posted to that URL with the payload and it was working and i was getting a response similar to what i could see in the developer.

这给了我一个“405 Not Allowed”错误。我使用 chrome 开发人员工具检查了 post 方法的详细信息,可以看到一个 api (URL/api/auth)。我使用有效负载发布到该 URL 并且它正在工作,并且我收到了类似于我在开发人员中看到的响应。

Unfortunately when trying to 'get' another url after login, i am still getting the content from the login page. Why is the login not sticking? Should i use cookies? I am a newbie, so i don't really know how to work with cookies.

不幸的是,在登录后尝试“获取”另一个 url 时,我仍然从登录页面获取内容。为什么登录不卡?我应该使用cookies吗?我是新手,所以我真的不知道如何使用 cookie。

回答by Freek Wiekmeijer

From the documentation:

文档

  1. get cookie from response

    url = 'http://example.com/some/cookie/setting/url'
    r = requests.get(url)
    r.cookies
    

    {'example_cookie_name': 'example_cookie_value'}

  2. give cookie back to server on subsequent request

    url = 'http://httpbin.org/cookies'
    cookies = dict(cookies_are='working')
    r = requests.get(url, cookies=cookies)`
    
  1. 从响应中获取 cookie

    url = 'http://example.com/some/cookie/setting/url'
    r = requests.get(url)
    r.cookies
    

    {'example_cookie_name': 'example_cookie_value'}

  2. 在后续请求时将 cookie 返回给服务器

    url = 'http://httpbin.org/cookies'
    cookies = dict(cookies_are='working')
    r = requests.get(url, cookies=cookies)`
    

回答by gtalarico

You can use a session object. It stores the cookies so you can make requests, and it handles the cookies for you

您可以使用会话对象。它存储 cookie 以便您可以提出请求,并为您处理 cookie

s = requests.Session() 
# all cookies received will be stored in the session object

s.post('http://www...',data=payload)
s.get('http://www...')

Docs: https://requests.readthedocs.io/en/master/user/advanced/#session-objects

文档:https: //requests.readthedocs.io/en/master/user/advanced/#session-objects

You can also save the cookie data to an external file, and then reload them to keep session persistent without having to login every time you run the script:

您还可以将 cookie 数据保存到外部文件,然后重新加载它们以保持会话持久性,而无需每次运行脚本时都登录:

How to save requests (python) cookies to a file?

如何将请求 (python) cookie 保存到文件中?