Dear Sir,
I tried to extract data like Current price, Total Market Cap, Free Float Market Cap, Macro-Economic Sector, Sector, Industry and Basic Industry from
I want to get the same kind of data for others using base file: https://archives.nseindia.com/content/equities/EQUITY_L.csv
I have written a code in which I was able to extract only 300 entries. I am not able to convert to csv or xls file.
import requests
import pandas as pd
import re
import os
import sys
class NseIndia:
def __init__(self):
self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'}
self.session = requests.Session()
self.session.get("http://nseindia.com", headers=self.headers)
def get_stock_info(self,symbol):
url1 = 'https://www.nseindia.com/api/quote-equity?symbol=' + symbol.replace(' ', '%20').replace('&','%26') + "§ion=trade_info"
url2 = 'https://www.nseindia.com/api/quote-equity?symbol=' + symbol.replace(' ', '%20').replace('&', '%26')
tc = self.session.get(url1, headers=self.headers).json()['marketDeptOrderBook']['tradeInfo']['totalMarketCap']
fc = self.session.get(url1, headers=self.headers).json()['marketDeptOrderBook']['tradeInfo']['ffmc']
lp = self.session.get(url2, headers=self.headers).json()['priceInfo']['lastPrice']
ma = self.session.get(url2, headers=self.headers).json()["industryInfo"]["macro"]
se = self.session.get(url2, headers=self.headers).json()["industryInfo"]["sector"]
ind = self.session.get(url2, headers=self.headers).json()["industryInfo"]["industry"]
bas = self.session.get(url2, headers=self.headers).json()["industryInfo"]["basicIndustry"]
return tc, fc, lp, ma, se, ind, bas
nse = NseIndia()
nifty50_url = 'https://archives.nseindia.com/content/equities/EQUITY_L.csv'
df_n50 = pd.read_csv(nifty50_url)
regexp = re.compile('&')
TC = []
FC = []
LP = []
MA = []
SE = []
IND = []
BAS = []
while True:
try:
for index, row in df_n50.iterrows():
symbol = row['SYMBOL']
if (regexp.search(symbol) != None):
symbol = symbol.replace('&', '%26')
TCap, FCap, LPrice, Macro, Sector, Industry, BIndustry = nse.get_stock_info(symbol)
TC.append(str(TCap))
FC.append(str(FCap))
LP.append(str(LPrice))
MA.append(str(Macro))
SE.append(str(Sector))
IND.append(str(Industry))
BAS.append(str(BIndustry))
os.system('cls')
print("|{:50s} | {:20s} | {:10s} | {:10s} | {:10s} | {:10s} | {:10s}| {:10s} | {:10s}| {:10s} | {:10s}|".format('Company Name',
'Symbol','Listing Date','Face value',
'TotalCap',
'FFCap',
'LastPricce',
'Macro',
'Sector',
'Industry',
"BasicInd"))
for index, row in df_n50.iterrows():
symbol = row['SYMBOL']
print("|{:50s} | {:20s} | {:10s} |{:10s} | {:10s} | {:10s} | {:10s} | {:10s} | {:10s} |{:10s} | {:10s} |".format(
str(row['NAME OF COMPANY']),
row['SYMBOL'],row[' DATE OF LISTING'], str(row[' FACE VALUE']),
TC[index].rjust(10),
FC[index].rjust(10),
LP[index].rjust(10),
MA[index].rjust(10),
SE[index].rjust(10),
IND[index].rjust(10),
BAS[index].rjust(10)))
except KeyboardInterrupt:
sys.exit()
Please help…
Best Regards,
S