This recipe is solely due to the programming of Corey Goldberg (corey@goldb.org). The only thing I did to it was make it into a class. I thought that by uploading it here that more people would come across it and more people could use it. It is really good. It gives you stock information.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95  | #!/usr/bin/env python
#
#  Copyright (c) 2007-2008, Corey Goldberg (corey@goldb.org)
#
#  license: GNU LGPL
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License, or (at your option) any later version.
#
# Made into class by Alexander Wallar on December 17, 2011
import urllib
class StockInfo:
    """
    Constructor
    """
    def __init__(self, __symbol):
        self.symbol = __symbol
        
    def __request(self, stat):
        url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (self.symbol, stat)
        return urllib.urlopen(url).read().strip().strip('"')
    
    def get_all(self):
        """
        Get all available quote data for the given ticker symbol.
        Returns a dictionary.
        """
        values = self.__request('l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7').split(',')
        data = {}
        data['price'] = values[0]
        data['change'] = values[1]
        data['volume'] = values[2]
        data['avg_daily_volume'] = values[3]
        data['stock_exchange'] = values[4]
        data['market_cap'] = values[5]
        data['book_value'] = values[6]
        data['ebitda'] = values[7]
        data['dividend_per_share'] = values[8]
        data['dividend_yield'] = values[9]
        data['earnings_per_share'] = values[10]
        data['52_week_high'] = values[11]
        data['52_week_low'] = values[12]
        data['50day_moving_avg'] = values[13]
        data['200day_moving_avg'] = values[14]
        data['price_earnings_ratio'] = values[15]
        data['price_earnings_growth_ratio'] = values[16]
        data['price_sales_ratio'] = values[17]
        data['price_book_ratio'] = values[18]
        data['short_ratio'] = values[19]
        return data
    get_price = lambda self: float(self.__request('l1'))    
    get_change = lambda self: float(self.__request('c1'))
    get_volume = lambda self: float(self.__request('v'))
    get_avg_daily_volume = lambda self: float(self.__request('a2'))
    get_stock_exchange = lambda self: float(self.__request('x'))    
    get_market_cap = lambda self: float(self.__request('j1')) 
    get_book_value = lambda self: float(self.__request('b4'))
    get_ebitda = lambda self: float(self.__request('j4'))
    get_dividend_per_share = lambda self: float(self.__request('d'))
    get_dividend_yield = lambda self: float(self.__request('y'))
    get_earnings_per_share = lambda self: float(self.__request('e'))
    get_52_week_high = lambda self: float(self.__request('k'))
    get_52_week_low = lambda self: float(self.__request('j'))
    get_50day_moving_avg = lambda self: float(self.__request('m3'))
    get_200day_moving_avg = lambda self: float(self.__request('m4'))
    get_price_earnings_ratio = lambda self: float(self.__request('r'))
    get_price_earnings_growth_ratio = lambda self: float(self.__request('r5'))
    get_price_sales_ratio = lambda self: float(self.__request('p5'))
    get_price_book_ratio = lambda self: float(self.__request('p6'))
    get_short_ratio = lambda self: float(self.__request('s7'))
        
    def get_historical_prices(self, start_date, end_date):
        """
        Get historical prices for the given ticker symbol.
        Date format is 'YYYYMMDD'
    
        Returns a nested list.
        """
        url = 'http://ichart.yahoo.com/table.csv?s=%s&' % self.symbol + \
              'd=%s&' % str(int(end_date[4:6]) - 1) + \
              'e=%s&' % str(int(end_date[6:8])) + \
              'f=%s&' % str(int(end_date[0:4])) + \
              'g=d&' + \
              'a=%s&' % str(int(start_date[4:6]) - 1) + \
              'b=%s&' % str(int(start_date[6:8])) + \
              'c=%s&' % str(int(start_date[0:4])) + \
              'ignore=.csv'
        days = urllib.urlopen(url).readlines()
        data = [day[:-2].split(',') for day in days]
        return data
 | 
Here is an example of how to use the code:
>>> googStock = StockInfo('GOOG')
>>> googStock.get_price()
625.96
>>> googStock.get_avg_daily_volume()
3181950
>>> googStock.get_historical_prices('20100101','20100130')
[['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Clos'],
 ['2010-01-29', '538.49', '540.99', '525.61', '529.94', '4140500', '529.9'],
 ['2010-01-28', '544.49', '547.00', '530.60', '534.29', '3229100', '534.2'],
 ['2010-01-27', '541.27', '547.65', '535.31', '542.10', '3964400', '542.1'],
 ['2010-01-26', '537.97', '549.60', '536.29', '542.42', '4355500', '542.4'],
 ['2010-01-25', '546.59', '549.88', '535.51', '540.00', '4419900', '540.0'],
 ['2010-01-22', '564.50', '570.60', '534.86', '550.01', '6800400', '550.0'],
 ['2010-01-21', '583.44', '586.82', '572.25', '582.98', '6307700', '582.9'],
 ['2010-01-20', '585.98', '585.98', '575.29', '580.41', '3250700', '580.4'],
 ['2010-01-19', '581.20', '590.42', '576.29', '587.62', '4316700', '587.6'],
 ['2010-01-15', '593.34', '593.56', '578.04', '580.00', '5434500', '580.0'],
 ['2010-01-14', '583.90', '594.20', '582.81', '589.85', '4240100', '589.8'],
 ['2010-01-13', '576.49', '588.38', '573.90', '587.09', '6496600', '587.0'],
 ['2010-01-12', '597.65', '598.16', '588.00', '590.48', '4853300', '590.4'],
 ['2010-01-11', '604.46', '604.46', '594.04', '601.11', '7212900', '601.1'],
 ['2010-01-08', '592.00', '603.25', '589.11', '602.02', '4724300', '602.0'],
 ['2010-01-07', '609.40', '610.00', '592.65', '594.10', '6414300', '594.1'],
 ['2010-01-06', '625.86', '625.86', '606.36', '608.26', '3978700', '608.2'],
 ['2010-01-05', '627.18', '627.84', '621.54', '623.99', '3004700', '623.9'],
 ['2010-01-04', '626.95', '629.51', '624.24', '626.75', '1956200', '626.7']]
THIS WAS ALL CREATED BY COREY GOLDBERG. I thought it would be more useful on activestate
Download
Copy to clipboard
Everything is good except this line googStock = stockInfo('GOOG')
The stockInfo should be StockInfo instead.
it goes in error because you pass self.symbol in values = self.__request(self.symbol, 'l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7').split(',')
Thank you paolo. I just fixed it