没有从YahooFinance获取数据

2024-04-26 14:36:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下问题。我是编程新手,目前正试图从雅虎导入财务数据进行分析。但由于某些原因,Python无法获取第一个ticker的数据。下面是出现问题的函数(save_sp500_tickers()函数工作正常):

def get_data_from_yahoo(reload_sp500 = False):

    if reload_sp500:
        tickers = save_sp500_tickers()
    else:
        with open("sp500tickers.pickle","rb") as f:
                tickers = pickle.load(f)
    if not os.path.exists("stock_dfs"):
        os.makedirs("stock_dfs")

    start = dt.datetime(2000, 1, 1)
    end = dt.datetime(2016, 12, 31)

    for ticker in tickers:
        print(ticker)
        if not os.path.exists("stock_df/{}.csv".format(ticker)):
            df = web.DataReader(ticker, "yahoo", start, end)
            df.to_csv("stock_df/{}.csv".format(ticker))
        else:
            print("Already have{}".format(ticker))


    get_data_from_yahoo()
Aaron's Inc.   

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas_datareader\yahoo\daily.py in _read_one_data(self, url, params)
    156             j = json.loads(re.search(ptrn, resp.text, re.DOTALL).group(1))
--> 157             data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
    158         except KeyError:

KeyError: 'HistoricalPriceStore'

During handling of the above exception, another exception occurred:

RemoteDataError                           Traceback (most recent call last)
<ipython-input-46-70d65e54c473> in <module>
----> 1 get_data_from_yahoo(reload_sp500=False)

<ipython-input-45-9c120fc94d41> in get_data_from_yahoo(reload_sp500)
     15         print(ticker)
     16         if not os.path.exists("stock_df/{}.csv".format(ticker)):
---> 17             df = web.DataReader(ticker, "yahoo", start, end)
     18             df.to_csv("stock_df/{}.csv".format(ticker))
     19         else:

~\Anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    206                 else:
    207                     kwargs[new_arg_name] = new_arg_value
--> 208             return func(*args, **kwargs)
    209 
    210         return wrapper

~\Anaconda3\lib\site-packages\pandas_datareader\data.py in DataReader(name, data_source, start, end, retry_count, pause, session, api_key)
    385             retry_count=retry_count,
    386             pause=pause,
--> 387             session=session,
    388         ).read()
    389 

~\Anaconda3\lib\site-packages\pandas_datareader\base.py in read(self)
    249         # If a single symbol, (e.g., 'GOOG')
    250         if isinstance(self.symbols, (string_types, int)):
--> 251             df = self._read_one_data(self.url, params=self._get_params(self.symbols))
    252         # Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
    253         elif isinstance(self.symbols, DataFrame):

~\Anaconda3\lib\site-packages\pandas_datareader\yahoo\daily.py in _read_one_data(self, url, params)
    158         except KeyError:
    159             msg = "No data fetched for symbol {} using {}"
--> 160             raise RemoteDataError(msg.format(symbol, self.__class__.__name__))
    161 
    162         # price data

RemoteDataError: No data fetched for symbol Aaron's Inc.
 using YahooDailyReader

如果有人能解释一下为什么会出现这个RemoteDataError,那就太好了

感谢德国的问候和问候


Tags: csvinselfformatdfdatagetif