简体   繁体   中英

How to get list of components in yahoo finance index using pandas?

So im trying to get data from multiple stocks from yahoo finance and write it to excell. The problem is at the moment i have to hardcode the stocks in question. and currently i would like to download the information from all 25 stocks in the C25 index (^OMXC25) or potentially other indexes. so therefore i would like to know how i can acces the components list and retrieve these and then download each of them. The current code i use to get each is as follows:

import pandas as pd
import pandas_datareader as pdr
import datetime as dt

download_source = (r'C:\Users\SKlin\Downloads\OMXC25.xlsx')

start = dt.datetime(2010,1,1)
end = dt.datetime.today()

writer = pd.ExcelWriter(download_source, engine ='xlsxwriter')

#GN Store Nord
dfGN = pdr.get_data_yahoo('GN.CO',start,end)
dfGN.to_excel(writer, sheet_name='GN.CO')       

#Vestas Wind systems 
dfVestas = pdr.get_data_yahoo('VWS.CO',start,end)
dfVestas.to_excel(writer, sheet_name='VWS.CO')     

writer.save()

This saves the data just fint, but with 25 stocks it's doable, but seems tidious to do with index with 500 stocks.. Plz help.

If you can get a list of all symbols:

stocks = ['GN.CO' , 'VWS.CO']
for stock in stocks:
    dfGN = pdr.get_data_yahoo(stock ,start,end)
    dfGN.to_excel(writer, sheet_name=stock )  

Use beautiful soup to scrape a list of the ticker names from wiki:

https://en.wikipedia.org/wiki/OMX_Copenhagen_25

Then just iterate through them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM