如何编写和附加Yahoo Finance URL的多个股票的库存数据?
我有以下代码,需要从Yahoo URL下载多个attrict的数据:
import time
import datetime
import pandas as pd
#read ticker symbols from a file to a python list object named ticker
symbols = []
with open('ticker_list.csv') as f:
symbol = [row.split()[0] for row in f]
f.close
period1 = int(time.mktime(datetime.datetime(2020, 12, 1, 23, 59).timetuple()))
period2 = int(time.mktime(datetime.datetime(2020, 12, 31, 23, 59).timetuple()))
interval = '1d' # 1d, 1wk, 1m
xlwriter = pd.ExcelWriter('Stock_Price_sample.xlsx', engine='xlsxwriter')
for ticker in symbols:
query_string = f'https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1={period1}&period2={period2}&interval={interval}&events=history&includeAdjustedClose=true'
df = pd.read_csv(query_string)
append_df.to_excel(xlwriter, sheet_name='Sheet1', index=False)
xlwriter.save()
例如,在ticker_list.csv文件中,我有以下记录:MSFT,AAPL,TSLA等。
我无法下载库存数据和库存数据和将它们附加到Excel作家。它给了我一个空白页。任何帮助如何解决此或替代技术都将不胜感激。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在此任务中使用Yahooquery或Yfinance。
You can use yahooquery or yfinance for this task.
安装yfinance
下载单股票程序,
在此处开始,结束日期,例如start ='2020-01-01',end ='2020-12-31',ticker ='aapl ='aapl'
对于多个脚架/符号,
输出将是dataframe带有多级索引。
还有更多详细信息答案 and 上的nofollow noreferrer”>。
Install yfinance
Downloading single ticker,
Here start, end are dates, say start='2020-01-01', end='2020-12-31', ticker='AAPL'
For multiple tickers/symbols,
The output would be dataframe with multilevel indices.
There are more details in this answer and on pypi.