yfinance 是一个使用 Yahoo! 获取数据的 Python 第三方模块。它支持获取最细到1分钟级的历史数据及股票基本面数据。

yfinance 是一个流行的开源库,由 Ran Aroussi 开发,用于访问雅虎财经上可用的财务数据。

雅虎财经提供了大量关于股票、债券、货币和加密货币的市场数据。 它还提供市场新闻、报告和分析,以及其他选项和基本面数据,使其与一些竞争对手区分开来。

Install

1
pip install yfinance

使用

使用 yfinance 快速抓取一些加密货币价格数据

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import pandas as pd
import yfinance as yf
from datetime import datetime, timedelta

end_time = datetime.now()
start_time = end_time - timedelta(days = 5)

# Yfinance doesn't have great minute data
data = yf.download(
        "BTC-USD", 
        start=start_time, 
        end=end_time,
        interval="1h")

print(data)

获取多个交易对数据

1
2
3
4
5
6
7
8
data = yf.download(
        "BTC-USD ETH-USD", 
        start=start_time, 
        end=end_time,
        interval="1h",
        group_by="ticker")

print(data)

通过定义一只股票的Ticker,利用info属性获取一只股票的基本数据,如市值、市盈率、股息等

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import yfinance as yf

aapl = yf.Ticker("aapl")
print(aapl.info)
# {'zip': '95014', 'sector': 'Technology' ...

# 市盈率(PE)
aapl.info['forwardPE']

# 新闻数据
print(aapl.news)

参考