解决Yahoo!finance数据接口无法访问的问题

解决Yahoo!finance数据接口无法访问的问题

Yahoo! finance早已停止提高他们的数据接口, 这导致了很多很好用的包不能再使用. fix-yahoo-finance包通过爬取雅虎财经并返回一个pandas的数据框临时性的解决了这个问题。 同时这个数据框的格式和pandas_datareader包的get_data_yahoo()函数获取的数据框格式相同。

这个包的使用方法如下:

1
2
3
4
5
from pandas_datareader import data as pdr
import fix_yahoo_finance as yf
yf.pdr_override()
data = pdr.get_data_yahoo("SPY", start="2017-01-01", end="2017-04-30")
data = pdr.get_data_yahoo(["SPY", "IWM"], start="2017-01-01", end="2017-04-30")

同时这个包还添加了一下其它的参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
data = pdr.get_data_yahoo(
# 股票代码列表
tickers = ["SPY", "IWM", "..."],
# 开始日期
start = "2017-01-01",
# 截止日期
end = "2017-04-30",
# 返回一个multi-index dataframe
# (optional, default is Panel, which is deprecated)
as_panel = False,
# group by ticker (to access via data['SPY'])
# (optional, default is 'column')
group_by = 'ticker',
# adjust all OHLC automatically
# (optional, default is False)
auto_adjust = True,
# download dividend + stock splits data
# (optional, default is None)
# options are:
# - True (returns history + actions)
# - 'only' (actions only)
actions = True,
# 线程数量
threads = 10
)

此外这个包还可以单独使用,例如下载上证指数的历史数据:

1
2
import fix_yahoo_finance as yf
data = yf.download("000001.ss", start="2017-01-01", end="2018-07-30")

# Python

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×