yfinance 是一个使用 Yahoo! 获取数据的 Python 第三方模块。它支持获取最细到1分钟级的历史数据及股票基本面数据,是免费获得美股分钟级及以上粒度数据的不二之选。
1.准备
请选择以下任一种方式输入命令安装依赖:
1. Windows 环境 打开 Cmd (开始-运行-CMD)。
2. MacOS 环境 打开 Terminal (command+空格输入Terminal)。
3. 如果你用的是 VSCode编辑器 或 Pycharm,可以直接使用界面下方的Terminal.
2.yfinance 基本使用
通过yfinance你可以使用一样命令下载任意美股股票的数据,比如:
import yfinance as yf
# 单股
data = yf.download("AAPL", start="2017-01-01", end="2017-04-30")
# Open High Low Close Adj Close Volume
# Date
# 2017-01-03 28.950001 29.082500 28.690001 29.037500 27.257641 115127600
# 2017-01-04 28.962500 29.127501 28.937500 29.004999 27.227135 84472400
# 2017-01-05 28.980000 29.215000 28.952499 29.152500 27.365593 88774400
# 2017-01-06 29.195000 29.540001 29.117500 29.477501 27.670671 127007600
# 2017-01-09 29.487499 29.857500 29.485001 29.747499 27.924126 134247600
# ......
# 多股
data = yf.download("SPY AAPL", start="2017-01-01", end="2017-04-30",
group_by="ticker")
# AAPL ... SPY
# Open High Low Close ... Low Close Adj Close Volume
# Date ...
# 2017-01-03 28.950001 29.082500 28.690001 29.037500 ... 223.880005 225.240005 205.509079 91366500
# 2017-01-04 28.962500 29.127501 28.937500 29.004999 ... 225.610001 226.580002 206.731735 78744400
# 2017-01-05 28.980000 29.215000 28.952499 29.152500 ... 225.479996 226.399994 206.567459 78379000
# 2017-01-06 29.195000 29.540001 29.117500 29.477501 ... 225.899994 227.210007 207.306549 71559900
# ......
默认是获取天级别的数据,如果你需要获取分钟级的,只需要添加interval参数:
import yfinance as yf
# 单股
data = yf.download("AAPL", start="2022-05-18", end="2022-05-23", interval="1m")
print(data)
# Open High Low Close Adj Close Volume
# Datetime
# 2022-05-17 12:00:00-04:00 148.000000 148.050003 147.839996 147.865005 147.865005 0
# 2022-05-17 12:01:00-04:00 147.869507 147.919998 147.779999 147.889893 147.889893 123746
# 2022-05-17 12:02:00-04:00 147.889999 147.929993 147.750000 147.907394 147.907394 92847
# 2022-05-17 12:03:00-04:00 147.904999 147.929993 147.785004 147.839996 147.839996 79266
# 2022-05-17 12:04:00-04:00 147.839996 147.895004 147.779999 147.860001 147.860001 58905
# ......
它支持的分钟级参数有:1m,2m,5m,15m,30m,60m,90m 等等。
此外还支持小时级和天线、周线、月线级别:1h,1d,5d,1wk,1mo,3mo 等等。
获取到的数据类型就是Dataframe,因此你还可以直接保存为csv文件:
# 公众号:Python 实用宝典
import yfinance as yf
data = yf.download("AAPL", start="2022-05-18", end="2022-05-23", interval="1m")
data.to_csv("aapl_20220518_20220523.csv")
# 保存到本地,命名为 aapl_20220518_20220523.csv
3.通过yfinance获取股票基本数据
如果你需要获取一只股票的基本数据,如市值、市盈率、股息等,你可以通过定义一只股票的Ticker,利用其info属性获取:
# 公众号:Python 实用宝典
import yfinance as yf
aapl = yf.Ticker("aapl")
print(aapl.info)
# {'zip': '95014', 'sector': 'Technology', 'fullTimeEmployees': 154000, 'longBusinessSummary': 'Apple ......
这个字典比较长,这里省略显示了,里面包含了比如市盈率(PE)等信息:
# 公众号:Python 实用宝典
import yfinance as yf
aapl = yf.Ticker("aapl")
aapl.info['forwardPE']
# 20.974085
你还可以获取每次派息数据:
# 公众号:Python 实用宝典
import yfinance as yf
aapl = yf.Ticker("aapl")
print(aapl.dividends)
# Date
# 1987-05-11 0.000536
# 1987-08-10 0.000536
# 1987-11-17 0.000714
# 1988-02-12 0.000714
# 1988-05-16 0.000714
# ...
# 2021-05-07 0.220000
# ... ...
获取资产负债表:
# 公众号:Python 实用宝典
import yfinance as yf
aapl = yf.Ticker("aapl")
print(aapl.balancesheet)
# 2021-09-25 2020-09-26 2019-09-28 2018-09-29
# Total Liab 2.879120e+11 2.585490e+11 2.480280e+11 2.585780e+11
# Total Stockholder Equity 6.309000e+10 6.533900e+10 9.048800e+10 1.071470e+11
# Other Current Liab 5.357700e+10 4.786700e+10 4.324200e+10 3.929300e+10
# Total Assets 3.510020e+11 3.238880e+11 3.385160e+11 3.657250e+11
# Common Stock 5.736500e+10 5.077900e+10 4.517400e+10 4.020100e+10
# ......
现金流数据:
# 公众号:Python 实用宝典
import yfinance as yf
aapl = yf.Ticker("aapl")
print(aapl.cashflow)
# 2021-09-25 2020-09-26 2019-09-28 2018-09-29
# Investments -2.819000e+09 5.335000e+09 5.809300e+10 3.084500e+10
# Change To Liabilities 1.400200e+10 -1.981000e+09 -2.548000e+09 9.172000e+09
# Total Cashflows From Investing Activities -1.454500e+10 -4.289000e+09 4.589600e+10 1.606600e+10
# ......
新闻数据:
# 公众号:Python 实用宝典
import yfinance as yf
aapl = yf.Ticker("aapl")
print(aapl.news)
# [{'uuid': '476a41c6-c6dc-3050-9b8f-c3777c8485b2', 'title': "Dow Jones Futures Rise After 'Hard' Reality Hits Market; What To Do Now", 'publisher': "Investor's Business Daily",
# 'link': 'https://finance.yahoo.com/m/476a41c6-c6dc-3050-9b8f-c3777c8485b2/dow-jones-futures-rise-after.html',
# 'providerPublishTime': 1653305573, 'type': 'STORY'}, {'uuid': '721d466d-5394-3f3c-a9c3-b0920d44c7f3' ......
总之,有了yfinance这个神器,除了高频数据你无法获取之外,其他的美股数据你都能获取得到,有需要的小伙伴可以试试,非常好用。
Python实用宝典 (pythondict.com)
不只是一个宝典
欢迎关注公众号:Python实用宝典