pandas 在python中从yahoo金融自动下载历史股票价格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12433076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download history stock prices automatically from yahoo finance in python
提问by Bob
Is there a way to automatically download historical prices of stocks from yahoo finance or google finance (csv format)? Preferably in Python.
有没有办法从雅虎财经或谷歌财经(csv格式)自动下载股票的历史价格?最好在 Python 中。
采纳答案by Joe C.
Short answer: Yes. Use Python's urllibto pull the historical data pages for the stocks you want. Go with Yahoo! Finance; Google is both less reliable, has less data coverage, and is more restrictive in how you can use it once you have it. Also, I believe Google specifically prohibits you from scraping the data in their ToS.
简短的回答:是的。使用 Python 的urllib来拉取你想要的股票的历史数据页面。与雅虎一起去!金融; Google 的可靠性较低,数据覆盖范围较小,而且一旦拥有它,您就可以使用它的方式受到更多限制。此外,我相信谷歌明确禁止您在其 ToS 中抓取数据。
Longer answer: This is the script I use to pull all the historical data on a particular company. It pulls the historical data page for a particular ticker symbol, then saves it to a csv file named by that symbol. You'll have to provide your own list of ticker symbols that you want to pull.
更长的答案:这是我用来提取特定公司的所有历史数据的脚本。它提取特定股票代码的历史数据页面,然后将其保存到以该代码命名的 csv 文件中。您必须提供自己想要提取的股票代码列表。
import urllib
base_url = "http://ichart.finance.yahoo.com/table.csv?s="
def make_url(ticker_symbol):
return base_url + ticker_symbol
output_path = "C:/path/to/output/directory"
def make_filename(ticker_symbol, directory="S&P"):
return output_path + "/" + directory + "/" + ticker_symbol + ".csv"
def pull_historical_data(ticker_symbol, directory="S&P"):
try:
urllib.urlretrieve(make_url(ticker_symbol), make_filename(ticker_symbol, directory))
except urllib.ContentTooShortError as e:
outfile = open(make_filename(ticker_symbol, directory), "w")
outfile.write(e.content)
outfile.close()
回答by Def_Os
When you're going to work with such time series in Python, pandas
is indispensable. And here's the good news: it comes with a historical data downloader for Yahoo: pandas.io.data.DataReader
.
当您要在 Python 中处理此类时间序列时,这pandas
是必不可少的。好消息是:它带有 Yahoo: 的历史数据下载器pandas.io.data.DataReader
。
from pandas.io.data import DataReader
from datetime import datetime
ibm = DataReader('IBM', 'yahoo', datetime(2000, 1, 1), datetime(2012, 1, 1))
print(ibm['Adj Close'])
Here's an example from the pandas
documentation.
Update for pandas >= 0.19:
大熊猫更新 >= 0.19:
The pandas.io.data
module has been removed from pandas>=0.19
onwards. Instead, you should use the separate pandas-datareader
package. Install with:
该pandas.io.data
模块已被移除pandas>=0.19
。相反,您应该使用单独的pandas-datareader
package。安装:
pip install pandas-datareader
And then you can do this in Python:
然后你可以在 Python 中做到这一点:
import pandas_datareader as pdr
from datetime import datetime
ibm = pdr.get_data_yahoo(symbols='IBM', start=datetime(2000, 1, 1), end=datetime(2012, 1, 1))
print(ibm['Adj Close'])
Downloading from Google Finance is also supported.
回答by MaxU
Extending @Def_Os'sanswer with an actual demo...
使用实际演示扩展@Def_Os 的答案...
As @Def_Os has already said - using Pandas Datareadermakes this task a real fun
正如@Def_Os 已经说过的 - 使用Pandas Datareader使这项任务变得非常有趣
In [12]: from pandas_datareader import data
pulling all available historical data for AAPL
starting from 1980-01-01
提取所有可用的历史数据以AAPL
从1980-01-01
#In [13]: aapl = data.DataReader('AAPL', 'yahoo', '1980-01-01')
# yahoo api is inconsistent for getting historical data, please use google instead.
In [13]: aapl = data.DataReader('AAPL', 'google', '1980-01-01')
first 5 rows
前 5 行
In [14]: aapl.head()
Out[14]:
Open High Low Close Volume Adj Close
Date
1980-12-12 28.750000 28.875000 28.750 28.750 117258400 0.431358
1980-12-15 27.375001 27.375001 27.250 27.250 43971200 0.408852
1980-12-16 25.375000 25.375000 25.250 25.250 26432000 0.378845
1980-12-17 25.875000 25.999999 25.875 25.875 21610400 0.388222
1980-12-18 26.625000 26.750000 26.625 26.625 18362400 0.399475
last 5 rows
最后 5 行
In [15]: aapl.tail()
Out[15]:
Open High Low Close Volume Adj Close
Date
2016-06-07 99.250000 99.870003 98.959999 99.029999 22366400 99.029999
2016-06-08 99.019997 99.559998 98.680000 98.940002 20812700 98.940002
2016-06-09 98.500000 99.989998 98.459999 99.650002 26419600 99.650002
2016-06-10 98.529999 99.349998 98.480003 98.830002 31462100 98.830002
2016-06-13 98.690002 99.120003 97.099998 97.339996 37612900 97.339996
save all data as CSV file
将所有数据保存为 CSV 文件
In [16]: aapl.to_csv('d:/temp/aapl_data.csv')
d:/temp/aapl_data.csv - 5 first rows
d:/temp/aapl_data.csv - 前 5 行
Date,Open,High,Low,Close,Volume,Adj Close
1980-12-12,28.75,28.875,28.75,28.75,117258400,0.431358
1980-12-15,27.375001,27.375001,27.25,27.25,43971200,0.408852
1980-12-16,25.375,25.375,25.25,25.25,26432000,0.378845
1980-12-17,25.875,25.999999,25.875,25.875,21610400,0.38822199999999996
1980-12-18,26.625,26.75,26.625,26.625,18362400,0.399475
...
回答by Naufal
There is already a library in Python called yahoo_finance so you'll need to download the library first using the following command line:
Python 中已经有一个名为 yahoo_finance 的库,因此您需要先使用以下命令行下载该库:
sudo pip install yahoo_finance
Then once you've installed the yahoo_finance library, here's a sample code that will download the data you need from Yahoo Finance:
然后,一旦您安装了 yahoo_finance 库,下面的示例代码将从 Yahoo Finance 下载您需要的数据:
#!/usr/bin/python
import yahoo_finance
import pandas as pd
symbol = yahoo_finance.Share("GOOG")
google_data = symbol.get_historical("1999-01-01", "2016-06-30")
google_df = pd.DataFrame(google_data)
# Output data into CSV
google_df.to_csv("/home/username/google_stock_data.csv")
This should do it. Let me know if it works.
这应该这样做。让我知道它是否有效。
UPDATE: The yahoo_finance library is no longer supported.
更新:不再支持 yahoo_finance 库。
回答by atreadw
You can check out the yahoo_fin package. It was initially created after Yahoo Finance changed their API (documentation is here: http://theautomatic.net/yahoo_fin-documentation).
您可以查看 yahoo_fin 包。它最初是在雅虎财经更改其 API 后创建的(文档在这里:http: //theautomatic.net/yahoo_fin-documentation)。
from yahoo_fin import stock_info as si
aapl_data = si.get_data("aapl")
nflx_data = si.get_data("nflx")
aapl_data.head()
nflx_data.head()
aapl.to_csv("aapl_data.csv")
nflx_data.to_csv("nflx_data.csv")