来自Yahoo的pandas_datareader.data奇怪的错误

时间:2018-12-14 13:59:47

标签: python pandas beautifulsoup python-requests pickle

请帮助我,当我运行代码时,假设从我的.pickle文件中获取4个字母的代码,并使用pandas_datareader从yahoo获取数据。但问题在于它一直说“没有为符号MMM提取任何数据”,但显然存在!我尝试用“ MMM”替换变量“ ticker”,它工作正常,但不适用于变量“ ticker”

def save_dow_tickers():
resp = requests.get('https://en.wikipedia.org/wiki/Dow_Jones_Industrial_Average') #Get source code from link
soup = bs.BeautifulSoup(resp.text, 'lxml') #Load source code to text using BeautifulSoup
table = soup.find('table', {'class':'wikitable sortable'}) #Find table called wikitable sortable
tickers = []
for row in table.findAll('tr')[1:]: #Go through every row in table excluding headers
    ticker = row.findAll('td')[2].text#Go and get the first column from every row
    tickers.append(ticker) #Load data from first column to ticker array
with open('dowtickers.pickle', 'wb') as f: #open file called save_sp500_tickers
    pickle.dump(tickers, f) #Dump ticker array in file
print(tickers)
return tickers
def get_data_from_yahoo(reload_dow=False): #Call another function from a       function
if reload_dow:
    tickers = save_dow_tickers()
else:
    with open('dowtickers.pickle', 'rb') as f: #open tickers file and read
        tickers = pickle.load(f)
if not os.path.exists('stock_dfs'): #Check if directory exists
    os.makedirs('stock_dfs') #Create directory
start = dt.datetime(2008,12,10)
end = dt.datetime(2018,12,10)
for ticker in tickers: #Retrieve data for every ticker in S&P 500
    print(ticker)
    if not os.path.exists('stock_dfs/{}.csv'.format(ticker)): #Check if data already exsists
        df = web.DataReader(ticker, 'yahoo', start, end) #Pull data from Yahoo
        df.to_csv('stock_dfs/{}.csv'.format(ticker))
    else:
        print('Already have {}'.format(ticker))
def get_data_from_yahoo(reload_dow=False): #Call another function from a       function
if reload_dow:
    tickers = save_dow_tickers()
else:
    with open('dowtickers.pickle', 'rb') as f: #open tickers file and read
        tickers = pickle.load(f)
if not os.path.exists('stock_dfs'): #Check if directory exists
    os.makedirs('stock_dfs') #Create directory
start = dt.datetime(2008,12,10)
end = dt.datetime(2018,12,10)
for ticker in tickers: #Retrieve data for every ticker in S&P 500
    print(ticker)
    if not os.path.exists('stock_dfs/{}.csv'.format(ticker)): #Check if data already exsists
        df = web.DataReader(ticker, 'yahoo', start, end) #Pull data from Yahoo
        df.to_csv('stock_dfs/{}.csv'.format(ticker))
    else:
        print('Already have {}'.format(ticker))

0 个答案:

没有答案