Python requests not returning cookies

Viewed 449

I am using Python3 requests for a simple cookie grab:

import requests

session = requests.Session()
response = session.get('https://www.radiotimes.com/tv/tv-listings/')

print(session.cookies.get_dict())

...this works for some sites, but not this particular one. What do I need to do differently?

Thanks

1 Answers

This page use Javascript to generate a cookie.

You will find when you send GET request,it will also send a GET request to a page to get cookie.

enter image description here

enter image description here In your example,maybe you need to send a GET request to that page.Then you could do what you want:

import requests

session = requests.Session()
response = session.get('https://radiotimes-gb.radiotimes.com/abbKC8hLX1VJCIxejAmMSJ6JzBaM1hMQXx-fiwyKzF6JiFYTFJSQXx9LVgxLzIiQS8iIyIvLyIvWEEnM1hNSUtJTEtLTkF-IX5YNCYrISw0SXowLXpJKjAkSXomKzEiLyt8KUl-IX5MQSUvIiNYJTExLTBATlxATWFATWE0NDRJL3whJiwxJioiMEl-LCpATWExM0BNYTEzSCkmMDEmKyQwQE1h')

print(session.cookies.get_dict())

# {'_sp_v1_csv': '2.0.1003', '_sp_v1_data': '2:0:1589036314:1:0:1:1:0:0:_:-1', '_sp_v1_lt': '1:ab|true:', '_sp_v1_opt': '1:', '_sp_v1_ss': 'null', '_sp_v1_uid': '1:785:ca679d88-38f6-4c0d-b7bf-8491e074776c'}

Or I suggest you use selenium,that's will be quite easy.

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.radiotimes.com/tv/tv-listings/")
cookie = {l["name"]:l["value"] for l in driver.get_cookies()}
print(cookie)
Related