雅虎!、Y!Finance和雅虎财经是雅虎公司的注册商标。
yfinance与雅虎公司没有任何关联,也未经其认可或审核。它是一个开源工具,使用雅虎公开可用的API,旨在用于研究和教育目的。
关于您使用实际下载数据的权利详情,请参阅雅虎的使用条款 (这里、 这里和 这里)。请记住 - 雅虎财经API仅供个人使用。
</td></tr></table><a target="new" href="https://pypi.python.org/pypi/yfinance"><img border=0 src="https://yellow-cdn.veclightyear.com/ab5030c0/110b8885-a740-4baa-ae8a-2aa888ddbb57.svg?style=flat" alt="Python版本"></a> <a target="new" href="https://pypi.python.org/pypi/yfinance"><img border=0 src="https://yellow-cdn.veclightyear.com/ab5030c0/bafa8ad7-d5fc-49a2-91fd-e85a63a9abc4.svg?maxAge=60%" alt="PyPi版本"></a> <a target="new" href="https://pypi.python.org/pypi/yfinance"><img border=0 src="https://yellow-cdn.veclightyear.com/ab5030c0/840ae4b4-db5a-4b3f-b737-6cf574fa3e40.svg?maxAge=60" alt="PyPi状态"></a> <a target="new" href="https://pypi.python.org/pypi/yfinance"><img border=0 src="https://yellow-cdn.veclightyear.com/ab5030c0/21ba795b-eaa9-4b76-b837-78c85612fc19.svg?maxAge=2592000&label=installs&color=%2327B1FF" alt="PyPi下载量"></a> <a target="new" href="https://travis-ci.com/github/ranaroussi/yfinance"><img border=0 src="https://img.shields.io/travis/ranaroussi/yfinance/main.svg?maxAge=1" alt="Travis-CI构建状态"></a> <a target="new" href="https://www.codefactor.io/repository/github/ranaroussi/yfinance"><img border=0 src="https://www.codefactor.io/repository/github/ranaroussi/yfinance/badge" alt="CodeFactor"></a> <a target="new" href="https://github.com/ranaroussi/yfinance"><img border=0 src="https://yellow-cdn.veclightyear.com/ab5030c0/4509605d-2c1e-45a5-b12b-8917e50c18fb.svg?style=social&label=Star&maxAge=60" alt="为此仓库加星"></a> <a target="new" href="https://twitter.com/aroussi"><img border=0 src="https://yellow-cdn.veclightyear.com/ab5030c0/dd0aba90-22a2-4df9-8000-1bd7e13e8e86.svg?style=social&label=Follow&maxAge=60" alt="在Twitter上关注我"></a>
yfinance提供了一种多线程且符合Python风格的方式来从雅虎财经下载市场数据。
→ 查看这篇博客文章,获取详细教程和代码示例。
使用pip
安装yfinance
:
$ pip install yfinance --upgrade --no-cache-dir
要安装可选依赖,将optional
替换为:nospam
用于更智能的抓取,repair
用于价格修复,或nospam,repair
同时安装两者:
$ pip install "yfinance[optional]"
Ticker
模块允许您以更符合Python风格的方式访问股票数据:
import yfinance as yf msft = yf.Ticker("MSFT") # 获取所有股票信息 msft.info # 获取历史市场数据 hist = msft.history(period="1mo") # 显示历史数据的元信息(需要先调用history()) msft.history_metadata # 显示公司行为(股息、拆分、资本利得) msft.actions msft.dividends msft.splits msft.capital_gains # 仅适用于共同基金和ETF # 显示股票数量 msft.get_shares_full(start="2022-01-01", end=None) # 显示财务报表: # - 利润表 msft.income_stmt msft.quarterly_income_stmt # - 资产负债表 msft.balance_sheet msft.quarterly_balance_sheet # - 现金流量表 msft.cashflow msft.quarterly_cashflow # 查看`Ticker.get_income_stmt()`了解更多选项 # 显示持股人 msft.major_holders msft.institutional_holders msft.mutualfund_holders msft.insider_transactions msft.insider_purchases msft.insider_roster_holders msfs.sustainability # 显示推荐 msft.recommendations msft.recommendations_summary msft.upgrades_downgrades # 显示未来和历史盈利日期,默认最多返回未来4个季度和过去8个季度。 # 注意:如果需要更多,请使用msft.get_earnings_dates(limit=XX)并增加limit参数。 msft.earnings_dates # 显示ISIN代码 - *实验性功能* # ISIN = 国际证券识别码 msft.isin # 显示期权到期日 msft.options # 显示新闻 msft.news # 获取特定到期日的期权链 opt = msft.option_chain('YYYY-MM-DD') # 可通过以下方式获取数据:opt.calls, opt.puts
如果你想使用代理服务器下载数据,请使用:
import yfinance as yf msft = yf.Ticker("MSFT") msft.history(..., proxy="代理服务器") msft.get_actions(proxy="代理服务器") msft.get_dividends(proxy="代理服务器") msft.get_splits(proxy="代理服务器") msft.get_capital_gains(proxy="代理服务器") msft.get_balance_sheet(proxy="代理服务器") msft.get_cashflow(proxy="代理服务器") msft.option_chain(..., proxy="代理服务器") ...
要初始化多个Ticker
对象,请使用
import yfinance as yf tickers = yf.Tickers('msft aapl goog') # 访问每个股票代码(示例) tickers.tickers['MSFT'].info tickers.tickers['AAPL'].history(period="1mo") tickers.tickers['GOOG'].actions
要将价格历史下载到一个表格中:
import yfinance as yf data = yf.download("SPY AAPL", period="1mo")
yf.download()
和Ticker.history()
有许多选项用于配置获取和处理。查看Wiki了解更多选项和详细信息。yfinance