如何编写和附加Yahoo Finance URL的多个股票的库存数据?

发布于 2025-01-28 19:25:14 字数 1015 浏览 2 评论 0 原文

我有以下代码,需要从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作家。它给了我一个空白页。任何帮助如何解决此或替代技术都将不胜感激。

I have the following code and need to download data for multiple tickers from yahoo url:

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()

For example, in the ticker_list.csv file I have the following ticker: MSFT, AAPL, TSLA, etc.

I am not able to download the stock data and append them to an excel writer. It gives me a blank page. Any help how to solve this or alternative technique will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不回头走下去 2025-02-04 19:25:14

您可以在此任务中使用Yahooquery或Yfinance。

You can use yahooquery or yfinance for this task.

微暖i 2025-02-04 19:25:14

安装yfinance

pip install -U yfinance

下载单股票程序,

yfinace.download(ticker, start, end)

在此处开始,结束日期,例如start ='2020-01-01',end ='2020-12-31',ticker ='aapl ='aapl'

对于多个脚架/符号,

yfinance.download('AAPL TSLA', start, end)

输出将是dataframe带有多级索引。

还有更多详细信息答案 and 上的nofollow noreferrer”>。

Install yfinance

pip install -U yfinance

Downloading single ticker,

yfinace.download(ticker, start, end)

Here start, end are dates, say start='2020-01-01', end='2020-12-31', ticker='AAPL'

For multiple tickers/symbols,

yfinance.download('AAPL TSLA', start, end)

The output would be dataframe with multilevel indices.

There are more details in this answer and on pypi.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文