How to Retrieve Cookies from a Python Requests GET Request?

Estimated read time 2 min read

When making a GET request using the Python Requests library, you can retrieve the cookies returned by the server using the cookies attribute of the response object. Here’s an example:

import requests

url = "https://example.com"

response = requests.get(url)

cookies = response.cookies

print(cookies)

In this example, we send a GET request to the URL “https://example.com” using the requests.get() method. The response object contains the cookies returned by the server in the cookies attribute.

You can access the cookies as a RequestsCookieJar object, which provides various methods and properties to work with cookies. For example, you can iterate over the cookies or access individual cookies by name.

To print all the cookies returned by the server, you can simply use print(cookies). This will display the cookies in a human-readable format.

If you want to access a specific cookie by name, you can use the get() method of the RequestsCookieJar object. For example:

specific_cookie = cookies.get("cookie_name")

Replace “cookie_name” with the name of the specific cookie you want to retrieve.

By using the cookies attribute of the response object, you can retrieve the cookies returned by a GET request made with the Requests library in Python.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply