历史股票数据错误

时间:2017-06-25 23:51:10

标签: python python-2.7

当我尝试从雅虎或谷歌(我已经尝试过两者)检索股票价格时,它会一直返回此错误。我不知道它意味着什么或如何解决它。我之前使用过此代码并且运行良好。你可以帮我解决这个错误和解决方案。感谢。

import datetime as dt
import pandas as pd
import pandas_datareader.data as web


start = dt.datetime(2000,1,1)
end = dt.datetime(2004,1,1)

df= web.DataReader('TSLA', 'yahoo', start, end)
print(df.head)

ConnectionError:HTTPConnectionPool(host ='ichart.finance.yahoo.com',port = 80):使用url超出最大重试次数:/table.csv?a = 0&amp ;ignore = .csv& s = TSLA& b = 1& e = 1& d = 0& g = d& f = 2004& c = 2000(由NewConnectionError引起(':无法建立新连接:[Errno 8] nodename或servname提供,或未知',) )

2 个答案:

答案 0 :(得分:1)

只需更改此部分:
df=web.DataReader("TSLA","yahoo",start,end)至:df=web.DataReader("TSLA","google",start,end)

这里的问题是雅虎搜索引擎。所以希望这可以解决这个问题。

答案 1 :(得分:0)

以下是我找到的解决方法:

# Define the instruments to download. We would like to see Apple, Microsoft and the S&P500 index.
tickers = ['AAPL', 'MSFT', 'SPY']

# Define which online source one should use
data_source = 'google'

# We would like all available data from 01/01/2000 until 12/31/2016.
start_date = '2010-01-01'
end_date = '2016-12-31'

# User pandas_reader.data.DataReader to load the desired data. As simple as that.
panel_data = data.DataReader(tickers, data_source, start_date, end_date)

# Getting just the adjusted closing prices. This will return a Pandas DataFrame
# The index in this DataFrame is the major index of the panel_data.
close = panel_data.ix['Close']

# Getting all weekdays between 01/01/2000 and 12/31/2016
all_weekdays = pd.date_range(start=start_date, end=end_date, freq='B')

# How do we align the existing prices in adj_close with our new set of dates?
# All we need to do is reindex close using all_weekdays as the new index
close = close.reindex(all_weekdays)

close.head(10)

来自http://www.learndatasci.com/python-finance-part-yahoo-finance-api-pandas-matplotlib/