------------------ 方法 一 -----------------
1. 安装 pandas_datareader
pip install pandas_datareader
2. 获取对应股票的信息
import pandas_datareader as pdr
pdr.get_data_yahoo(‘AAPL’)
原文地址:https://blog.csdn.net/jinruoyanxu/article/details/79154224
import pandas.io.data as web
提示警告,pandas.io.data 已经 转移到了一个单独的包里面了,于是 按照提示 安装了 pandas_datareader,方法是在cmd环境下 pip install pandas_datareader,然后在命令行里面输入 下面那行就ok了
import pandas_datareader.data as web
pandas_datareader是一个远程获取金融数据的Python工具,通过它可以方便获得下面公司和机构的数据:
Yahoo! Finance//雅虎金融
Google Finance//谷歌金融
Enigma//Enigma是一个公共数据搜索的提供商
St.Louis FED (FRED)//圣路易斯联邦储备银行
Kenneth French’s data library//肯尼斯弗兰奇资料库
World Bank//世界银行
OECD//经合组织
Eurostat//欧盟统计局
Thrift Savings Plan//美国联邦政府管理离退休的组织
Oanda currency historical rate //外汇经纪商
Nasdaq Trader symbol definitions //纳斯达克
pandas-datareader安装非常简单:
$ pip install pandas-datareader
使用说明:
0.19.0开始, pandas不支持以前的pandas.io.data或pandas.io.wb包,如果你看见网上和书里使用 import pandas.io.data as web
你需要替换pandas.io为pandas_datareader。
如:
from pandas.io import data, wb # becomes
from pandas_datareader import data, wb
我们使用pandas_datareader获得苹果股票信息:
import pandas_datareader as pdr
pdr.get_data_yahoo(‘AAPL’)
当然你也可以换成你喜欢的股票。
------------------ 方法 二 -----------------
1. 安装 fix_yahoo_finance
pip install fix_yahoo_finance
2. Python 调用
import pandas_datareader.data as web import fix_yahoo_finance as fy #start = datetime.datetime(2010,1,1) #end = datetime.datetime(2017,1,1) fy.pdr_override() #df = web.get_data_yahoo('SPY','yahoo','20100101', '20140101') all_data = {} for ticker in ['AAPL', 'GOOG']: all_data[ticker] = web.get_data_yahoo(ticker, '2010-1-1', '2010-2-1') print(all_data['AAPL'])
另外,查看 https://pypi.org/project/fix-yahoo-finance/
Requirements
- Python >=3.4
- Pandas (tested to work with >=0.18.1)
- Numpy >= 1.11.1
- requests >= 2.14.2
- multitasking >= 0.0.3
Quick Start
from pandas_datareader import data as pdr
import fix_yahoo_finance as yf
yf.pdr_override() # <== that's all it takes :-)
# download dataframe
data = pdr.get_data_yahoo("SPY", start="2017-01-01", end="2017-04-30")
# download Panel
data = pdr.get_data_yahoo(["SPY", "IWM"], start="2017-01-01", end="2017-04-30")
I’ve also added some options to make life easier :)
Below is the full list of acceptable parameters:
data = pdr.get_data_yahoo(
# tickers list (single tickers accepts a string as well)
tickers = ["SPY", "IWM", "..."],
# start date (YYYY-MM-DD / datetime.datetime object)
# (optional, defaults is 1950-01-01)
start = "2017-01-01",
# end date (YYYY-MM-DD / datetime.datetime object)
# (optional, defaults is Today)
end = "2017-04-30",
# return a 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,
# How may threads to use?
threads = 10
)
It can also be used as a stand-alone library (without pandas_datareader) if you want:
import fix_yahoo_finance as yf
data = yf.download("SPY", start="2017-01-01", end="2017-04-30")
Installation
Install fix_yahoo_finance using pip:
$ pip install fix_yahoo_finance --upgrade --no-cache-dir
Requirements
- Python >=3.4
- Pandas (tested to work with >=0.18.1)
- Numpy >= 1.11.1
- requests >= 2.14.2
- multitasking >= 0.0.3