Last active
November 4, 2024 19:05
-
-
Save j-thepac/bf6714aafbb3a75536a88c4614e64cb5 to your computer and use it in GitHub Desktop.
Requests and Cookies with Example of NSE - Stock Exchange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Session Management with API | |
import requests,httpx | |
def bse(): | |
reqUrl = "https://api.bseindia.com/RealTimeBseIndiaAPI/api/GetSensexData/w" | |
headersList = {"Referer": "https://www.bseindia.com/" } | |
response = requests.GET(reqUrl, headers=headersList) | |
print(response.text) | |
def nse(): | |
reqUrl = "https://www.nseindia.com/api/marketStatus" | |
headersList = { | |
"Accept": "*/*", | |
"Accept-Language": "en-US,en;q=0.9,hi;q=0.8", | |
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" | |
} | |
response = requests.GET(reqUrl,headers=headersList) | |
print(response.text) | |
headers={ | |
'Host': 'www.nseindia.com', | |
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:85.0) Gecko/20100101 Firefox/85.0', | |
'Accept': '*/*', | |
} | |
def method1_ManualCookies(): | |
""" | |
1. goto nseIndia (any Page) | |
2. In the Browser > Developer > Network > find response > Reponse Headers >Under "Set-Cookie" , copy "bm_sv=09xxxx" | |
3. c={"bm_sv":"09xxxx"} | |
Or In Api Client add below as Headers: | |
'Host': 'www.nseindia.com', | |
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:85.0) Gecko/20100101 Firefox/85.0', | |
'Accept': '*/*', | |
'Cookie':bm_sv=xxxxx | |
( If its taking lot of time delete old headers , next to response tab ) | |
""" | |
resp=requests.get(url='https://www.nseindia.com/api/home-board-meetings?index=equities' | |
headers=headers,cookies=c,timeout=10) | |
print(resp.json()) | |
def method2_ApiCookies(): | |
resp=requests.get(url='https://www.nseindia.com/',headers=headers,timeout=10) | |
url_main='https://www.nseindia.com/api/historical/cm/equity?series=[%22EQ%22]&symbol=RELIANCE&from=04-10-2023&to=24-11-2023' | |
resp=requests.get(url=url_main,headers=headers,cookies=resp.cookies.get_dict(),timeout=10) | |
print(resp.json()) | |
def method3_sessionCookies(): | |
session = requests.Session() | |
session.get('https://www.nseindia.com/', headers=headers) | |
url_main='https://www.nseindia.com/api/historical/cm/equity?series=[%22EQ%22]&symbol=RELIANCE&from=04-10-2023&to=24-11-2023' | |
resp = session.get(url_main, headers=headers) | |
print(resp.json()) | |
method1_ManualCookies() | |
method2_ApiCookies() | |
method3_sessionCookies() | |
bse() | |
nse() | |
""" Additonal Headers incase needed | |
headers= { | |
"Host": "www1.nseindia.com", | |
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0", | |
"Accept": "*/*", | |
"Accept-Language": "en-US,en;q=0.5", | |
"Accept-Encoding": "gzip, deflate, br", | |
"X-Requested-With": "XMLHttpRequest", | |
"Referer": "https://www1.nseindia.com/products/content/equities/equities/eq_security.htm", | |
"Access-Control-Allow-Origin": "*", | |
"Access-Control-Allow-Methods": "GET,POST,PUT,DELETE,OPTIONS", | |
"Access-Control-Allow-Headers": "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With", | |
'Content-Type': 'application/start_date-www-form-urlencoded; charset=UTF-8' | |
} | |
headers={ | |
{ | |
'Host': 'www.nseindia.com', | |
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:85.0) Gecko/20100101 Firefox/85.0', | |
'Accept': '*/*', | |
'Accept-Language': 'en-US,en;q=0.5', | |
'Accept-Encoding': 'gzip, deflate, br', | |
'X-Requested-With': 'XMLHttpRequest', | |
'DNT': '1', | |
'Connection': 'keep-alive', | |
} | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment