Python cookie to dict

When working with web scraping and interacting with web pages, cookies can play an important role in maintaining the state of a session or authenticating a user. The Python requests library provides a simple way to add and manage cookies through its built-in cookiejar object. In this guide, we will discuss how to add a cookie to the cookiejar in the requests library.

Method 1: Using the requests.utils.cookiejar_from_dict() method

To add a cookie to the cookiejar in Python Requests library using the requests.utils.cookiejar_from_dict() method, follow these steps:

Step 1: Import the required libraries

import requests from http.cookiejar import CookieJar from requests.utils import cookiejar_from_dict

Step 2: Create a dictionary of cookies

cookies =  'cookie1': 'value1', 'cookie2': 'value2', 'cookie3': 'value3' >

Step 3: Create an empty cookie jar

Step 4: Use requests.utils.cookiejar_from_dict() to populate the cookie jar with the cookies from the dictionary

cookie_jar = cookiejar_from_dict(cookies)

Step 5: Make a request using the cookie jar

response = requests.get('https://www.example.com', cookies=cookie_jar)

Here is the complete code example:

import requests from http.cookiejar import CookieJar from requests.utils import cookiejar_from_dict cookies =  'cookie1': 'value1', 'cookie2': 'value2', 'cookie3': 'value3' > cookie_jar = CookieJar() cookie_jar = cookiejar_from_dict(cookies) response = requests.get('https://www.example.com', cookies=cookie_jar)

That’s it! You have successfully added cookies to the cookie jar using the requests.utils.cookiejar_from_dict() method.

Method 2: Using the requests.utils.add_dict_to_cookiejar() method

To add a cookie to the cookiejar in Python requests library using the requests.utils.add_dict_to_cookiejar() method, follow these steps:

import requests from requests.utils import cookiejar_from_dict, add_dict_to_cookiejar
cookies = 'cookie_name': 'cookie_value'>
cookie_jar = requests.cookies.RequestsCookieJar()
add_dict_to_cookiejar(cookie_jar, cookies)
response = requests.get('https://example.com', cookies=cookie_jar)
import requests from requests.utils import cookiejar_from_dict, add_dict_to_cookiejar cookies = 'cookie_name': 'cookie_value'> cookie_jar = requests.cookies.RequestsCookieJar() add_dict_to_cookiejar(cookie_jar, cookies) response = requests.get('https://example.com', cookies=cookie_jar)

You can add multiple cookies to the cookies dictionary and they will all be added to the cookiejar.

Method 3: Using the requests.Session() object

Here are the steps to add a cookie to the cookiejar in Python Requests Library using requests.Session():

cookie_dict = 'cookie_name': 'cookie_value'>
session.cookies.update(cookie_dict)

Here is the complete code example:

import requests session = requests.Session() cookie_dict = 'cookie_name': 'cookie_value'> session.cookies.update(cookie_dict)

This will add the cookie to the cookiejar using requests.Session(). You can verify the cookie by printing the cookies in the session object:

This will print the cookiejar, which should include the cookie that you just added:

RequestsCookieJar[Cookie cookie_name=cookie_value for .example.com/>]>

That’s it! You have successfully added a cookie to the cookiejar in Python Requests Library using requests.Session().

To add a cookie to the cookiejar in Python requests library using the requests.cookies.create_cookie() method, follow these steps:

import requests from requests.cookies import create_cookie
cookie_jar = requests.cookies.RequestsCookieJar()
cookie = create_cookie(name='cookie_name', value='cookie_value')
cookie_jar.set_cookie(cookie)

Here is the complete code:

import requests from requests.cookies import create_cookie cookie_jar = requests.cookies.RequestsCookieJar() cookie = create_cookie(name='cookie_name', value='cookie_value') cookie_jar.set_cookie(cookie) print(cookie_jar)
Cookie cookie_name=cookie_value for />]>

That’s it! You have successfully added a cookie to the cookiejar in Python requests library using the requests.cookies.create_cookie() method.

Источник

In Fiddler, I captured an HTTPS request with the following cookie string sent from the client (visible in Inspectors > Raw):

Cookie: devicePixelRatio=1; ident=exists; __utma=13103r6942.2918; __utmc=13103656942; __utmz=13105942.1.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); mp_3cb27825a6612988r46d00tinct_id%22%3A%201752338%2C%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fwww.pion_created_at%22%3A%20%222015-08-03%22%2C%22platform%22%3A%20%22web%22%2C%%22%3A%20%%22%7D; t_session=BAh7DUkiD3Nlc3NpbWVfZV9uYW1lBjsARkkiH1BhY2lmaWMgVGltZSAoVVMgJiBDYW5hZGEpBjsAVEkiFXNpZ25pbl9wZXJzb25faWQGOwBGaQMSvRpJIhRsYXN0X2xvZ2luX2RhdGUGOwBGVTogQWN0aXZlU3VwcG9ydDo6VGltZVdpdGhab25lWwhJdToJVGltZQ2T3RzAAABA7QY6CXpvbmVJIghVVEMGOwBUSSIfUGFjaWZpZWRfZGFzaGJvYXJkX21lc3NhZ2UGOwBGVA%3D%3D--6ce6ef4bd6bc1a469164b6740e7571c754b31cca 

I’d like to use this cookie in a Python Requests request. (I modified the cookie slightly, so that it can’t be used by readers for nefarious purposes!).

However, Requests appears to use a dictionary format for sending cookies, and I’m having trouble converting the above string/blob into a dictionary format.

  • Is there an automated way to convert a string (like the cookie I captured in Fiddler) into a dictionary in Python?

Solution 1: [1]

You should be able to use SimpleCookie which is available in the standard Python library:

from http.cookies import SimpleCookie rawdata = 'devicePixelRatio=1; ident=exists; __utma=13103r6942.2918; __utmc=13103656942; __utmz=13105942.1.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); mp_3cb27825a6612988r46d00tinct_id%22%3A%201752338%2C%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fwww.pion_created_at%22%3A%20%222015-08-03%22%2C%22platform%22%3A%20%22web%22%2C%%22%3A%20%%22%7D; t_session=BAh7DUkiD3Nlc3NpbWVfZV9uYW1lBjsARkkiH1BhY2lmaWMgVGltZSAoVVMgJiBDYW5hZGEpBjsAVEkiFXNpZ25pbl9wZXJzb25faWQGOwBGaQMSvRpJIhRsYXN0X2xvZ2luX2RhdGUGOwBGVTogQWN0aXZlU3VwcG9ydDo6VGltZVdpdGhab25lWwhJdToJVGltZQ2T3RzAAABA7QY6CXpvbmVJIghVVEMGOwBUSSIfUGFjaWZpZWRfZGFzaGJvYXJkX21lc3NhZ2UGOwBGVA%3D%3D--6ce6ef4bd6bc1a469164b6740e7571c754b31cca' cookie = SimpleCookie() cookie.load(rawdata) # Even though SimpleCookie is dictionary-like, it internally uses a Morsel object # which is incompatible with requests. Manually construct a dictionary instead. cookies =

If you are using Python 2, you will have to import from Cookie instead of http.cookies .

Solution 2: [2]

Try this function, definitly it will help :). This function supports more cookie attributes like comment, priority, version and Max-Age

def Robotcookie(cookie: str, parent_domain: str): items = cookie.split(';') SameSite = HttpOnly = Secure = Domain = Path = Expires = Comment = MaxAge = CookieName = CookieValue = Size = Sessionkey = Version = Priority = None CookieName = CookieValue = None idx = len(items) - 1 while idx >= 0: item = items[idx].strip() idx -= 1 if not item: continue SameSiteMatched = re.match(r'^SameSite(.*)?', item, re.I) HttpOnlyMatched = SameSiteMatched or re.match(r'^HttpOnly(.*)$', item, re.I) SecureMatched = HttpOnlyMatched or re.match(r'^Secure(.*)$', item, re.I) DomainMatched = SecureMatched or re.match(r'^Domain(.*)?', item, re.I) PathMatched = DomainMatched or re.match(r'^Path(.*)?', item, re.I) ExpiresMatched = PathMatched or re.match(r'^Expires(.*)?', item, re.I) CommentMatched = ExpiresMatched or re.match(r'^Comment(.*)?', item, re.I) MaxAgeMatched = ExpiresMatched or re.match(r'^Max-Age=(.*)?', item, re.I) VersionMatched = MaxAgeMatched or re.match(r'^Version=(.*)?', item, re.I) PriorityMatched = VersionMatched or re.match(r'^priority=(.*)?', item, re.I) matched = SameSiteMatched or HttpOnlyMatched or SecureMatched or DomainMatched or PathMatched or ExpiresMatched or CommentMatched or MaxAgeMatched or VersionMatched or PriorityMatched if matched: val = matched.groups(0)[0].lstrip('=') if matched == SameSiteMatched: SameSite = val if val.lower() in ['strict', 'lax', 'none'] else None elif matched == HttpOnlyMatched: HttpOnly = True elif matched == SecureMatched: Secure = True elif matched == DomainMatched: Domain = val elif matched == PathMatched: Path = val elif matched == PathMatched: Path = val elif matched == ExpiresMatched: Expires = val elif matched == CommentMatched: Comment = val elif matched == MaxAgeMatched: MaxAge = val elif matched == VersionMatched: Version = val elif matched == PriorityMatched: Priority = val else: CookieMatched = re.match(r'^(.[^=]*)=(.*)?', item, re.I) if CookieMatched: CookieName, CookieValue = CookieMatched.groups(0) Sessionkey = True if not Expires else False Size = (len(CookieName) if CookieName else 0) + (len(CookieValue) if CookieValue else 0) Domain = parent_domain if CookieName and not Domain else Domain Path = '/' if CookieName and not Path else Path Priority = 'Medium' if CookieName and not Priority else Priority.title() if Priority else 'Medium' Cookie = < CookieName: CookieValue, 'Domain': Domain, 'Path': Path, 'Expires': Expires, 'Comment': Comment, 'MaxAge': MaxAge, 'SameSite': SameSite, 'HttpOnly': HttpOnly, 'Secure': Secure, 'Size': Size, 'Sessionkey': Sessionkey, 'Version': Version, 'Priority': Priority >return Cookie if CookieName else None 
cookie = 'name=bijaya; comment=Comment1; expires=Mon, 26-Jul-2021 06:34:02 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=none; Max-Age=244114; Version=1.2; priority=high;' CookieDict = Robotcookie(cookie, parent_domain='google.com') 

Solution 3: [3]

For a plain cookie string ( document.cookies )

cookies = (dict(i.split('=') for i in rawdata.split('; '))) 

Источник

How To Handle Cookies With Python Requests Module

In interface testing, most projects have interfaces that require user login to operate, and the python requests module is often used to simulate login and post-login operations. In this article, I will show you two methods to handle login certificate cookies use python requests module.

1. Use requests.utils.dict_from_cookiejar() Method To Converts Returned Cookies Into A Dictionary.

# Import python requets module. import requests def login_return_cookie(): # The website login url. login_url = 'http://www.test-abc.com/login' # Custom request headers. headers = < "Accept": "application/json, text/javascript, */*; q=0.01" ># The login form submit data. login_data = < "username": "jerry", "password": "888888" >try: # Send a post request to login_url with login_data. response = requests.post(url=login_url, headers=headers, data=login_data) # Get server response cookies. cookies = response.cookies # Convert cookies to a dictionary object. cookie = requests.utils.dict_from_cookiejar(cookies) # Return the dictionary object. return cookie except Exception as err: # When there are exceptions, print error message. print('Retrieve cookie fail:\n'.format(err))
import requests # This function will use server returned cookie for later url request. def get_data(): # First invoke login_return_cookie() method to login to the website and get server returned cookies value. cookie = login_return_cookie() # Request below url to vote, the vote action need login cookies. vote_url = 'http://www.test-abc.com/vote' # Pass server returned cookies to the vote_url request. response = requests.get(url = vote_url, cookies=cookie) print(response.text)

2. Iterate Cookies Key-Value Pairs And Concat Them Into Cookie Format String.

# Import python requets module. import requests def login_return_cookie_string(): # The website login url. login_url = 'http://www.test-abc.com/login' # Custom request headers. headers = < "Accept": "application/json, text/javascript, */*; q=0.01" ># The login form submit data. login_data = < "username": "jerry", "password": "888888" >try: # Send a post request to login_url with login_data. response = requests.post(url=login_url, headers=headers, data=login_data) # Get server response cookies item. cookies = response.cookies.items() cookie = '' # Iterate the server returned cookies. for key, value in cookies: # Concat cookie string use the key=value; cookie format. cookie += '=;'.format(key, value) # Return the dictionary object. return cookie except Exception as err: # When there are exceptions, print error message. print('Retrieve cookie fail:\n'.format(err))
import requests def get_data(): # Get server response cookie string. cookie_string = login_return_cookie_string() # Set the cookie string in http request headers. headers = < "cookie": cookie_string ># Send get request with above custom headers(contains the cookies). response = requests.get(url=get_data_url, headers=headers) print(response.text)

Leave a Comment Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Источник

Читайте также:  Pandas python для чайников
Оцените статью