模块“ pandas_datareader.data”没有属性“ Datareader”

时间:2019-05-27 22:49:43

标签: python-3.x pandas yahoo-finance

我正在尝试运行以下代码,我正在使用Spyder。我还有另一个也使用pandas_datareader的文件,但我也从未在该文件上收到错误消息,该文件也在Spyder上运行。另外,另一个文件也使用yahoo,所以我也不认为这是问题所在。我的问题是:我的代码中是否有某些特殊内容导致该文件没有数据读取器模块?

import bs4 as bs
import datetime as dt
import os
import pandas_datareader.data as web
import pickle
import requests

def save_sp500_tickers():
     resp=requests.get('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
    soup=bs.BeautifulSoup(resp.text, 'lxml')
    table=soup.find('table',{'class':'wikitable sortable'})
    tickers=[]
    for row in table.findAll('tr')[1:]:
        ticker=row.findAll('td')[0].text
        tickers.append(ticker)

    with open('sp500tickers.pickle','wb') as f:
        pickle.dump(tickers, f)
    print(tickers)

    return tickers
#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(2016,1,1)
    end=dt.datetime(2019,5,27)

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

1 个答案:

答案 0 :(得分:0)

尝试一下:

from pandas_datareader import data, wb

然后使用:

data.DataReader('path') 

读取文件