如何在Python请求中使用cookie

use*_*157 33 python cookies python-requests

我正在尝试登录页面并访问页面中的其他链接.

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

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

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

gta*_*ico 53

您可以使用会话对象.它存储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...')
Run Code Online (Sandbox Code Playgroud)

文档:http://docs.python-requests.org/en/latest/user/advanced/

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

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

  • 它只是在应用程序运行时将它们保留在内存中。要从磁盘保存/加载cookie,请参阅:/sf/answers/2130880181/ (3认同)
  • :O 这太棒了!我希望我以前知道这个功能。它是像浏览器一样将 cookie 存储在磁盘上,还是只是将它们保存在内存中? (2认同)

Fre*_*jer 21

文档:

  1. 从响应中获取cookie

    url = 'http://example.com/some/cookie/setting/url'
    r = requests.get(url)
    r.cookies
    
    Run Code Online (Sandbox Code Playgroud)

    {'example_cookie_name': 'example_cookie_value'}

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

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

  • 身份验证 cookie 的正常流程是: (1) 当您提交登录表单时,您会在响应标头中收到一个 cookie。(2) 在后续页面请求中,您将 cookie 添加到请求标头中。 (2认同)
  • kwarg“cookies”是一个字典,您可以添加任意数量的项目。 (2认同)

cri*_*fan 7

总结(@Freek Wiekmeijer,@gtalarico)其他人的回答:

登录逻辑

  • 许多资源(页面,api)需要authentication,然后才能访问,否则405 Not Allowed
  • 常见的authentication=grant access方法有:
    • cookie
    • auth header
      • Basic xxx
      • Authorization xxx

如何使用cookierequests与AUTH

  1. 首先获取/生成cookie
  2. 为以下请求发送 cookie
  • 手动设置cookieheaders
  • cookierequests's 自动处理
    • session 自动管理cookies
    • response.cookies 手动设置 cookie

userequestssession自动管理cookies

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

payload={'username': "yourName",'password': "yourPassword"}
curSession.post(firstUrl, data=payload)
# internally return your expected cookies, can use for following auth

# internally use previously generated cookies, can access the resources
curSession.get(secondUrl)

curSession.get(thirdUrl)
Run Code Online (Sandbox Code Playgroud)

手动控制requestsresponse.cookies

payload={'username': "yourName",'password': "yourPassword"}
resp1 = requests.post(firstUrl, data=payload)

# manually pass previously returned cookies into following request
resp2 = requests.get(secondUrl, cookies= resp1.cookies)

resp3 = requests.get(thirdUrl, cookies= resp2.cookies)
Run Code Online (Sandbox Code Playgroud)


小智 6

正如其他人指出的,这是如何将 cookie 作为字符串变量添加到 headers 参数的示例 -

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) ...",
    "cookie": "_fbp=fb.1.1654447470850.2143140577; _ga=GA1.2.1...",
}
response = requests.get(url, headers=headers)
Run Code Online (Sandbox Code Playgroud)