I wrote the following code to retrieve data about stocks in the S&P 500. The code works, but it is very slow due to the number of urlopen requests. What strategies can I use to speed this up?
from urllib.request import urlopen
import csv
class StockQuote:
"""gets stock data from Yahoo Finance"""
def __init__(self, quote):
self.quote = quote
def lastPrice(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=l1'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def volume(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=v0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def yearrange(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=w0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def PEratio(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=r0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def bookValue(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=b4'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def EBITDA(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=j4'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def PEGRatio(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=r5'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def ticker(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=s0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def openSP500file():
SP500 = csv.reader(open(r'C:\Users\dev\Desktop\SP500.csv', 'r'), delimiter=',')
for x in SP500:
indStk = x[0]
printdata(indStk)
def printdata(stk):
stkObj = StockQuote(stk)
stkdata= {}
stkdata['Ticker'] = stkObj.ticker()
stkdata['Price'] = stkObj.lastPrice()
stkdata['PE Ratio'] = stkObj.PEratio()
stkdata['Volume'] = stkObj.volume()
stkdata['Year Range'] = stkObj.yearrange()
stkdata['Book Value per Share'] = stkObj.bookValue()
stkdata['EBITDA'] = stkObj.EBITDA()
stkdata['PEG Ratio'] = stkObj.PEGRatio()
print(stkdata)
def main():
openSP500file()
if __name__ == '__main__':
main()
Thanks!
You can use
threadingormultiprocessingmodule to fetch all those URLs in same time, so you can save a lot of time since the fetching are all individual not related to others.