简体   繁体   English

如何使用熊猫获取雅虎金融指数中的组件列表?

[英]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.所以我试图从雅虎财经的多只股票中获取数据并将其写入excel。 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.目前我想从 C25 指数 (^OMXC25) 或其他潜在指数中的所有 25 只股票中下载信息。 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.这只是节省了数据,但是对于 25 只股票它是可行的,但是对于包含 500 只股票的指数似乎很乏味......请帮忙。

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:使用美丽的汤从 wiki 中抓取股票代码名称列表:

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

Then just iterate through them.然后只需遍历它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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