title: using urllib.request to get stock data
body: <p>I wrote the following code to retrieve data about stocks in the S&amp;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?</p>
  
  <pre><code>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}&amp;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}&amp;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}&amp;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}&amp;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}&amp;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}&amp;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}&amp;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}&amp;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()
  </code></pre>
  
  <p>Thanks!</p>