python pandas yahoo stock data error

穿精又带淫゛_ 提交于 2019-12-12 06:39:06

问题


i am try to pullout intraday aapl stock data by yahoo. but there problem i facing with my program..

import pandas as pd 
import datetime
import urllib2
import matplotlib.pyplot as plt
get = 'http://chartapi.finance.yahoo.com/instrument/1.0/aapl/chartdata;type=quote;range=1d/csv'
getdata = urllib2.urlopen(get).read()
df = pd.read_csv(getdata, skiprows=17, header=None)
print df.head()

and error is this....

Traceback (most recent call last):
File "getyahoodata.py", line 10, in <module>
df = pd.read_csv(getdata, skiprows=16, header=None)
File "/usr/lib/python2.7/dist-packages/pandas/io/parsers.py", line 420, in parser_f
return _read(filepath_or_buffer, kwds)
File "/usr/lib/python2.7/dist-packages/pandas/io/parsers.py", line 218, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/usr/lib/python2.7/dist-packages/pandas/io/parsers.py", line 502, in __init__
self._make_engine(self.engine)
File "/usr/lib/python2.7/dist-packages/pandas/io/parsers.py", line 610, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/usr/lib/python2.7/dist-packages/pandas/io/parsers.py", line 972, in __init__
self._reader = _parser.TextReader(src, **kwds)
File "parser.pyx", line 330, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:3200)
File "parser.pyx", line 557, in pandas.parser.TextReader._setup_parser_source  (pandas/parser.c:5559)
IOError: File uri:/instrument/1.0/aapl/chartdata;type=quote;range=1d/csv

ticker:aapl

1413811857,98.3800,98.4999,98.3000,98.3800,1327900
1413811908,98.5200,98.6196,98.3360,98.3800,380100
1413811978,98.4200,98.5300,98.3850,98.4700,993800

:::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::
more and more


1413835019,99.8800,99.9100,99.8600,99.9000,524300
1413835079,99.8600,99.8850,99.8500,99.8700,600400
1413835139,99.8100,99.8600,99.7900,99.8500,530900
1413835199,99.7201,99.8301,99.7000,99.8200,1001500
1413835200,99.7600,99.7600,99.7600,99.7600,1720500

does not exist

in yahoo data pulling program show full data...but when i am useing google it work perfect...please try above program help to write program...thanks...


回答1:


pd.read_csv accepts a path or a filelike object. You're passing the text itself, and it's trying to read that as a filename. You can just pass the URL (although get isn't a great variable name..)

In [102]: df = pd.read_csv(get, skiprows=17, header=None)

In [103]: df.head()
Out[103]: 
            0        1        2        3        4        5
0  1413811859  98.3800  98.5000  98.3100  98.4800  1348400
1  1413811860  98.4775  98.6196  98.3265  98.3701   452200
2  1413811977  98.4250  98.5400  98.3800  98.4700   900000
3  1413812039  98.4800  98.4900  98.3900  98.4250   378100
4  1413812040  98.8300  98.8500  98.4700  98.4800   495300


来源:https://stackoverflow.com/questions/26475435/python-pandas-yahoo-stock-data-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!