Piers Thomas
Piers Thomas

Reputation: 327

Python - Print specific cookie value from request

Code:

from bs4 import BeautifulSoup
import requests

#GET SESSION
s = requests.Session()
s.get('https://www.clos19.com/en-gb/')

#GET CSRFToken
r = requests.get('https://www.clos19.com/en-gb/login')
soup = BeautifulSoup(r.text, 'html.parser')
CSRFToken = soup.find('input', type='hidden')["value"]
print(soup.find('input', type='hidden')["value"])

#ADD TO CART 
payload = {
    'productCode': '100559',
    'qty': '1',
    'CSRFToken': CSRFToken
}

r = s.post('https://www.clos19.com/en-gb/ajax/addToCart', data=payload, allow_redirects=False)
print(r.status_code)
print(s.cookies.get_dict())

Terminal output:

{'JSESSIONID': '448858956754C2F8F2DCF1CC4B803833', 'ROUTEID': '.app1', 'mhPreferredCountry': 'GB', 'mhPreferredLanguage': 'en_GB'}

How do I print only the JSESSIONID? Therefore the desired printed value would be 448858956754C2F8F2DCF1CC4B803833

Upvotes: 0

Views: 1476

Answers (1)

AdamKG
AdamKG

Reputation: 14091

change the last line to

print(s.cookies['JSESSIONID'])

Upvotes: 2

Related Questions