How to use cookies in Python Requests
You can use a session object. It stores the cookies so you can make requests, and it handles the cookies for you
s = requests.Session() # all cookies received will be stored in the session objects.post('http://www...',data=payload)s.get('http://www...')
Docs: 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:
From the documentation:
get cookie from response
url = 'http://example.com/some/cookie/setting/url'r = requests.get(url)r.cookies
{'example_cookie_name': 'example_cookie_value'}
give cookie back to server on subsequent request
url = 'http://httpbin.org/cookies'cookies = dict(cookies_are='working')r = requests.get(url, cookies=cookies)`
Summary (@Freek Wiekmeijer, @gtalarico) other's answer:
Logic of Login
- Many resource(pages, api) need
authentication
, then can access, otherwise405 Not Allowed
- Common
authentication
=grant access
method are:cookie
auth header
Basic xxx
Authorization xxx
How use cookie
in requests
to auth
- first get/generate cookie
- send cookie for following request
- manual set
cookie
inheaders
- auto process
cookie
byrequests
'ssession
to auto manage cookiesresponse.cookies
to manually set cookies
use requests
's session
auto manage cookies
curSession = requests.Session() # all cookies received will be stored in the session objectpayload={'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 resourcescurSession.get(secondUrl)curSession.get(thirdUrl)
manually control requests
's response.cookies
payload={'username': "yourName",'password': "yourPassword"}resp1 = requests.post(firstUrl, data=payload)# manually pass previously returned cookies into following requestresp2 = requests.get(secondUrl, cookies= resp1.cookies)resp3 = requests.get(thirdUrl, cookies= resp2.cookies)