简体   繁体   中英

Unable to retrieve Yahoo Finance stock data using pandas_datareader in Python

I run the following code:

import pandas_datareader.data as web
from datetime import date, datetime, timedelta
#
datetime_now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
todays_date = date.today()   # retreived in YYYY-MM-DD format
n = 7
date_n_days_ago = date.today() - timedelta(days=n)
#
mylist = ['AAPL', 'BEML.NS', 'BAJAJ-AUTO.NS', 'M&M.NS', 'JINDALSTEL.NS', 'L&TFH.NS']
#
for yahoo_symbol in mylist:
    try:
        stock_data = web.DataReader(yahoo_symbol, 'yahoo', date_n_days_ago, todays_date)
        print "success in retreiving data for: ",yahoo_symbol
    except:
        print "failed in retreiving data for: ",yahoo_symbol

This is the output that I receive:

success in retreiving data for:  AAPL
success in retreiving data for:  BEML.NS
failed in retreiving data for:  BAJAJ-AUTO.NS
failed in retreiving data for:  M&M.NS
success in retreiving data for:  JINDALSTEL.NS
failed in retreiving data for:  L&TFH.NS

My Doubt: Why am I unable to retrieve yahoo stock data using pandas_datareader when the symbol is "BAJAJ-AUTO.NS" or "M&M.NS", but can do the same when the symbol is " AAPL" or "BEML.NS". Is there any work-around to retreive this data ?

PS: I have noticed that whenever the stock symbol cotains an "&" or "-" , I am unable to retreive that data from Yahoo finance using pandas_datareader

Thank you.

try this

import math
import pandas_datareader as web 
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import  Dense, LSTM
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM