关于python:从Yahoo!加载数据 与熊猫融资

Loading data from Yahoo! Finance with pandas

我正在通过Wes McKinney的书"Python For Data Analysis"和第139页"Correlation and Covariance"中的工作,当我尝试运行他的代码从Yahoo!获取数据时,我收到了一个错误。 金融。

这是我正在运行的:

1
2
3
4
5
6
7
8
9
10
11
#CORRELATION AND COVARIANCE
import pandas.io.data as web

all_data = {}
for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOG']:
    all_data[ticker] = web.get_data_yahoo(ticker, '1/1/2003', '1/1/2013')

price = DataFrame({tic: data['Adj Close']
                   for tic, data in all_data.iteritems()})
volume = DataFrame({tic: data['Volume']
                    for tic, data in all_data.iteritems()})

这是我得到的错误:

1
2
3
4
5
6
7
8
9
10
11
12
Traceback (most recent call last):
  File"<stdin>", line 2, in <module>
  File"C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 390, in get_data_yahoo
    adjust_price, ret_index, chunksize, 'yahoo', name)
  File"C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 336, in _get_data_from
    hist_data = src_fn(symbols, start, end, retry_count, pause)
  File"C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 190, in _get_hist_yahoo
    return _retry_read_url(url, retry_count, pause, 'Yahoo!')
  File"C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 169, in _retry_read_url
   "return a 200 for url %r" % (retry_count, name, url))
IOError: after 3 tries, Yahoo! did not return a 200 for url 'http://ichart.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2000&d=0&e=1&f=2010&g=d&ignore=.csv'
>>> ... >>> >>> ... >>>

知道问题是什么吗?


正如卡尔指出的那样,自动收报机改变了雅虎返回"找不到页面"的含义。

从Web上轮询数据时,最好将try包裹在一起,除外

1
2
3
4
5
6
7
8
9
10
all_data = {}
for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOG']:
    try:
        all_data[ticker] = web.get_data_yahoo(ticker, '1/1/2003', '1/1/2013')
        price = DataFrame({tic: data['Adj Close']
                    for tic, data in all_data.iteritems()})
        volume = DataFrame({tic: data['Volume']
                    for tic, data in all_data.iteritems()})
    except:
        print"Cant find", ticker


截至17/1/17,我从本页和其他几个方面拼凑了以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from pandas_datareader import data as web
# import pandas.io.data as web
import fix_yahoo_finance
import datetime

start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2017, 6, 1)

all_data={}
for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOGL']:
    all_data[ticker] = web.get_data_yahoo(ticker, start, end)

price = DataFrame({tic: data['Adj Close']
                   for tic, data in all_data.iteritems()})
volume = DataFrame({tic: data['Volume']
                     for tic, data in all_data.iteritems()})


一旦你按照这些说明从pandas.io.data切换到pandas_datareader.data,同样的问题并将'GOOG'改为'GOOGL'似乎有效。

http://pandas-datareader.readthedocs.org/en/latest/remote_data.html#yahoo-finance


我使用下面的代码片段来加载雅虎财务数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import pandas_datareader as pdr
from datetime import datetime
from pandas import DataFrame as df

def get_data(selection, sdate, edate):
    data = pdr.get_data_yahoo(symbols=selection, start=sdate, end=edate)
    data =  df(data['Adj Close'])
    return data

start_date = datetime(2017, 1, 1)
end_date = datetime(2019,4,28)

selected = [ 'TD.TO', 'AC.TO', 'BNS.TO', 'ENB.TO', 'MFC.TO','RY.TO','BCE.TO']

print(get_data(selected, start_date, end_date).head(1))

https://repl.it/repls/DevotedBetterAlgorithms