In the last installment, we covered the basics of using Requests, and in this installment, we’ll continue our discussion of using cookies through Requests
What is a cookie?
HTTP requests themselves are stateless, meaning that if you visit a site one time, it’s no different than if you visit the same site the next. That is, there is no way to tell if one visit is the same person as the next. If so, there is no way to achieve account login, because you log in this second, the next second website will no longer know you. Therefore, it is important that the browser remember some key information so that you can bring it with you the next time you request the site to stay logged in
1. How to obtain cookies from Requests?
import requests
url = "http://www.baidu.com"
r = requests.get(url)
print(r.cookies)
Copy the code
With R.Cookie, you get a RequestsCookieJar object that holds cookie information
The object behaves like a dictionary, and you can get the value for the key or turn it into a dictionary through dict
2. How do I send your cookie to the server?
Pass the cookies parameter to the dictionary
import requests
url = "http://httpbin.org/cookies"
cookies = {"name": "xialaodi"}
r = requests.get(url, cookies=cookies)
print(r.text)
Copy the code
3. How to use RequestsCookieJar objects to send your cookies
You first need to construct a RequestsCookieJar object
import requests
url = "http://httpbin.org/cookies"
jar = requests.cookies.RequestsCookieJar()
jar.set("name"."xialaodi", domain="httpbin.org", path="/cookies")
r = requests.get(url, cookies=jar)
print(r.text)
Copy the code
You can also turn a dictionary directly into a RequestsCookieJar object using the requests.utils.cookiejar_from_dict() method
import requests
url = "http://httpbin.org/cookies"
cookies = {"name": "xialaodi"}
jar = requests.utils.cookiejar_from_dict(cookies)
r = requests.get(url, cookies=jar)
print(r.text)
Copy the code
You can also continue adding cookies to the RequestsCookieJar object using the request.utils.add_dict_to_cookiejar() method
import requests
url = "http://httpbin.org/cookies"
cookies = {"name": "xialaodi"}
jar = requests.utils.cookiejar_from_dict(cookies)
r = requests.get(url, cookies=jar)
print(r.text)
new_cookies = {"password": "123456"}
requests.utils.add_dict_to_cookiejar(jar, new_cookies)
r = requests.get(url, cookies=jar)
print(r.text)
Copy the code
4. Use Session object Session
Session objects allow you to keep some data on a site multiple times, including automatic cookies
import requests
s = requests.Session()
url = "http://httpbin.org/cookies/set/name/xialaodi"
r = s.get(url)
print(r.text)
url = "http://httpbin.org/cookies"
r = s.get(url)
print(r.text)
r = s.get(url, cookies={"password": "123456"})
print(r.text)
r = s.get(url)
print(r.text)
Copy the code
From the execution result of this code, we can see that the cookies returned to us by the website are recorded and kept by the session object, but the cookies we set through the cookies parameter are not kept by the session object
Therefore, if you want to manually add and persist cookies, use a RequestsCookieJar object
You can also use Session() via the with context manager
import requests
url = "http://httpbin.org/get"
with requests.Session() as s:
s.headers.update({"user-agent": "lsp"})
r = s.get(url)
print(r.text)
Copy the code