You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用yfinance获取Yahoo Finance小时级股票数据报错求助

问题原因与解决方法

1. 参数使用错误(核心原因)

你混淆了yfinance里period和interval的作用:

  • period是指定数据的时间跨度(比如取最近1天、1年的数据),它的可选值确实是报错里列出的那些,不支持1h;
  • 而小时级这种数据粒度,需要用interval参数来指定,可选值包括1m、5m、1h、1d等。

2. 免费数据的时间范围限制

Yahoo Finance的免费小时级数据仅开放最近7天内的内容,超过这个范围的小时数据需要付费订阅Yahoo Finance Premium才能获取。

修正后的代码

import yfinance as yf
from datetime import datetime, timedelta

tickerData = yf.Ticker('AAPL')
# 取最近7天的小时级数据,interval指定为1h
today_hourly = tickerData.history(period='7d', interval='1h')
print(today_hourly)

# 如果只想取今天的小时数据,可以限制start和end
today = datetime.today().date()
today_hourly = tickerData.history(start=f"{today} 00:00:00", end=f"{today} 23:59:59", interval='1h')
print(today_hourly)

补充说明

你参考的帖子可能是基于旧版本的yfinance,当时参数规则和现在不同。当前版本的yfinance严格区分了时间跨度(period)和数据间隔(interval),需要按新规则传参。

内容的提问来源于stack exchange,提问作者Another_coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 12:01:00