How to get stock data for many symbols by using python
May 4, 2022
If any one tried to straight way that is creating list of stock symbols then use for loop for each stock to get the data, you will end up with getting error because api does not allow you to request too many times.
My trick is to use sleep function in python to prevent this error
import pandas as pd
from pandas_datareader import data as pdr#you can use different start date
start = dt.datetime(2021,5,4)
now = dt.datetime.now()stock_list = []##symb_list1 contains stock symbolsfor i in range(len(symb_list1)):
try:
print(symb_list1[i])
df = pdr.get_data_yahoo(symb_list1[i], start,now)
df[‘Symbol’] = symb_list1[i]
stock_list.append(df)
except Exception as e:
print(‘Error’)
time.sleep(60*3) ##you can use different sleep time