1

I am unable to get cookies from the below code.

I tried a different scraper but no progress.

Is there anyone with an idea on how to achieve it?

import requests
session = requests.Session()
print(session.cookies.get_dict())
response = session.get('https://example.com')
print(session.cookies.get_dict())

Any suggestions is appreciated with any method.

Thanks

Zac
  • 323
  • 7
  • 14

1 Answers1

2

Some sites, including this one apparently, don't return any cookies if it doesn't look like the request is originating from a browser.

You need to pass some headers to make it look like the request is coming from a browser -- the User-agent is often enough to get you started.

As an example, consider this:

import requests

session = requests.Session()
print(session.cookies.get_dict())

# Impersonate Firefox
headers = {
    'User-agent': 'Mozilla/5.0'
}

response = session.get('https://nseindia.com', headers=headers)
print(session.cookies.get_dict())
# Should now give you the cookies

There is more discussion on setting the User-agent in this post.

costaparas
  • 5,047
  • 11
  • 16
  • 26