Pulling dynamic stock from an API code

by: Mike123, 17 days ago


So I've been trying to pull dynamic stocks, provided from the yahoo API using the following code provided in one of the tutorials:

try:
        print('Currently Pulling',stock)
        urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10y/csv'
        stockFile =[]
        try:
            sourceCode = urllib.request.urlopen(urlToVisit).read().decode()
            splitSource = sourceCode.split('n')
            for eachLine in splitSource:
                splitLine = eachLine.split(',')
                if len(splitLine)==6:
                    if 'values' not in eachLine:
                        stockFile.append(eachLine)
        except Exception as e:
            print(str(e), 'failed to organize pulled data.')
    except Exception as e:
        print(str(e), 'failed to pull pricing data')

while True:
    stock = input('Stock to plot: ')
    graphData(stock,10,50)


As a result, when I enter a value such as; 'AAPL', or 'TSLA' this error would pop up:
<urlopen error [Errno 11001] getaddrinfo failed> failed to organize pulled data.


If anyone could supply me with a way to pull stock data from an API, that'd be highly appreciated.



You must be logged in to post. Please login or register an account.