我目前正在创建一个程序,我想用yahoo_finance模块从雅虎财经股票获取数据。不过,我想用我认为将是一个循环的4股获取数据。下面是我想用的基本结构:
from yahoo_finance import Share
ticker_symbols = ["YHOO", "GOOG", "AAPL"]
i = 0
while i < 4:
company = Share(str(i))
print (company.get_open())
i += 1
我需要获得帮助的主要问题是我怎么构建一个循环遍历所有的ticker_symbols。正如你可以告诉我的“尝试”上面的我完全无言以对,因为我是新来的蟒蛇。我的第二个问题是,我怎么会从30天前取数据最多使用模块当前日期。也许我应该诉诸网页抓取,但它似乎很困难得多。
遍历你可以做一个列表:
for symbol in ticker_symbols :
company = Share(symbol)
这是基本的Python!我会建议你遵循一个小教程学习Python的基础知识。
您可以使用Share(symbol).get_historical('aDate')
得到历史的每日数据。在这里,你可以找到所有的包中可用的方法:https://pypi.python.org/pypi/yahoo-finance
祝你好运
您需要遍历ticker_symbols
列表,只需沟while循环:
from yahoo_finance import Share
ticker_symbols = ["YHOO", "GOOG", "AAPL"]
for i in ticker_symbols:
company = Share(i)
print (company.get_open())