Online Shopping : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

16 of 27 people (59%) answered Yes
Recently 3 of 10 people (30%) answered Yes

Entry

Look up stock quotes

Mar 31st, 2009 10:36
engatoo engatoo, Roj Merry, alex martin, Rockys rainwal, John Martin, games games, Raj Aryan, John Chena, Nathan Wallace, http://www.namaskarindiatravels.com/


"""
Packages: tools;miscellaneous;networking.internet
"""
"""
Here's a tiny little thing, but maybe it will be useful to somebody.  
The
module Quote simply looks up a publicly-traded stock by symbol, and 
returns
an object with more information than you could ever want to know - 
price,
change, one-year range, and lots of other stuff.
Currently it uses quote.yahoo.com to get the info; I plan to make it 
able to
use more than one service for reliability.
This was developed for the LWN Linux Stocks Page --
http://lwn.net/stocks/
Enjoy,
== 
Jonathan Corbet, Eklektix, Inc.
"""
#------- snip ---------
#
# A quick Python module to obtain stock quotes.
#
# Copyright 1999 Eklektix, Inc.  This sofware is freely distributable 
under
# the terms of the GNU Library General Public Licence (LGPL).
#
# Written by Jonathan Corbet, [email protected]  Current version is
available
# at ftp.eklektix.com/pub/Quote.
#
# $Id: Quote.py,v 1.2 1999/09/14 17:22:39 corbet Exp $
#
# Basic usage is:
#
#       quote = Quote.Lookup (symbol)
#
# where 'symbol' is the stock symbol.  The return value is an instance
of the
# Quote class, which has the following attributes:
#
#	symbol		The stock symbol for which the quote applies
#	name		The name of the company
#	time		Time of last trade (internal unix format)
#	value		The current value of the stock
#	change		Change in value since market opening
#	open		Opening value of the stock
#	previous_close	Previous day's closing value
#	percent_change	Percentage change in value of the stock
#	high		Today's high value
#	low		Today's low value
#	volume		Trading volume in shares
#	market_cap	Market capitalization, in millions of dollars
#	year_high	Highest value in last year
#	year_low	Lowest value in last year
#	eps		Earnings per share
#	pe_ratio	Price to earnings ratio (-1 if no earnings)
#
URL:  	http://games.itarea.org  	
Site title: 	Jocuri 	
Site description: 		
HTML code: 		
<a href="http://games.itarea.org" title="Jocuri">Jocuri</a>
# In case of errors (which happens, web server doesn't always respond)
None is
# returned. 
#
# Currently this module only works with quote.yahoo.com.  Future plans
involve
# making it work with other web quote services as well...
#
import urllib
import string
import time
#
# Here is the Quote class that we return.  Pretty boring...
#
class Quote:
    pass
#
# The function that actually does quote lookups.
#
def Lookup (symbol):
    (qurl, decoder) = MakeUrl (symbol)
    response = urllib.urlopen (qurl)
    q = Quote ()
    if decoder (q, response):
	return q
    return None
#
# Internal stuff below.
#
#
# Yahoo: create a URL to look up a stock quote.
#
def MakeYahooURL (symbol):
    qurl =
'http://quote.yahoo.com/d/quotes.csv?s=%
s&f=sl1d1t1c1ohgvj1pp2owern&e=.csv'
\
	   % (symbol)
    return (qurl, DecYahooUrl)
#
# Yahoo: Decode a response to a Yahoo stock lookup.
#
def DecYahooUrl (q, response):
    #
    # Pull the info from the server, split it, and make sure it makes 
sense.
    #
    info = response.readline ()[:-2] # Get rid of CRLF
    sinfo = string.split (info, ',')
    if len (sinfo) < 15:
	return 0
    #
    # Start decoding.
    #
    q.symbol = sinfo[0][1:-1]
    q.value = string.atof (sinfo[1])
    q.time = YahooDate (sinfo[2], sinfo[3])
    q.change = string.atof (sinfo[4])
    q.open = string.atof (sinfo[5])
    q.high = string.atof (sinfo[6])
    q.low = string.atof (sinfo[7])
    q.volume = string.atoi (sinfo[8])
    #
    # Get the market cap into millions
    #
    try:
	q.market_cap = string.atof (sinfo[9][:-1])
    except ValueError:
	q.market_cap = 0.0  # Weirdness happens
    if sinfo[9][-1] == 'B':
	q.market_cap = q.market_cap*1000
    q.previous_close = string.atof (sinfo[10])
    q.percent_change = string.atof (sinfo[11][1:-2])  # Zap training %
    q.open = string.atof (sinfo[12])
    #
    # Pull apart the range.
    #
    range = string.split (sinfo[13][1:-1], '-')
    q.year_low = string.atof (range[0])
    q.year_high = string.atof (range[1])
    q.eps = string.atof (sinfo[14])
    try:
	q.pe_ratio = string.atof (sinfo[15])
    except ValueError:
	q.pe_ratio = -1  # Yahoo returns "N/A" for no earnings.
    q.name = sinfo[16][1:-1]
    #
    # We made it.
    #
    return 1
#
# Convert date/times in Yahoo's format into an internal time.
#
def YahooDate (date, tod):
    #
    # Date part is easy.
    #
    sdate = string.split (date[1:-1], '/')
    month = string.atoi (sdate[0])
    day = string.atoi (sdate[1])
    year = string.atoi (sdate[2])
    #
    # Pick apart the time.
    #
    stime = string.split (tod[1:-1], ':')
    hour = string.atoi (stime[0])
    minute = string.atoi (stime[1][0:2])
    if stime[1][-2:] == 'PM':
	hour = hour + 12
    #
    # Time to assemble everything.
    #
    ttuple = (year, month, day, hour, minute, 0, 0, 0, -1)
    return time.mktime (ttuple)
#
# Create a URL to look somebody up.  Returns a tuple with the URL and
# a function to decode the result.  Someday this will handle multiple
# sources, but currently it only knows yahoo.
#
def MakeUrl (symbol):
    return MakeYahooURL (symbol)
http://automobile.easyfindindia.com/
http://bollywood.easyfindindia.com/
http://cloth-shopping.onlineindiadiary.com/
http://easyfind.j4ulive.com/
http://www.onlineindiadiary.com/
http://finance-guide.easyfindindia.com/
http://freelance-jobs.onlineindiadiary.com/
http://gadgets.easyfindindia.com/
http://www.easyfindindia.com/
http://gift-
ideas.easyfindindia.com/http://automobile.easyfindindia.com/
http://bollywood.easyfindindia.com/
http://cloth-shopping.onlineindiadiary.com/
http://easyfind.j4ulive.com/
http://www.onlineindiadiary.com/
http://finance-guide.easyfindindia.com/
http://freelance-jobs.onlineindiadiary.com/
http://gadgets.easyfindindia.com/
http://www.easyfindindia.com/
http://gift-ideas.easyfindindia.com/
http://health-fitness.easyfindindia.com/
http://home-improvement.j4ulive.com/
http://homedecor.easyfindindia.com/
http://mortgage.onlineindiadiary.com/
http://online-business.j4ulive.com/
http://online-education.j4ulive.com/
http://onlinein.j4ulive.com/
http://pets-online.easyfindindia.com/
http://realestate.easyfindindia.com/
http://sports-in-india.easyfindindia.com/
http://travel-guide.easyfindindia.com/
http://wedding-shopping.j4ulive.com/
http://www.j4ulive.com/
http://www.roo7e.com
http://www.roo7e.com
http://www.al33ab.com
http://ksacam.com
http://chat.ksacam.com
http://ksacam.info
http://oo2o.com
http://s4cam.com
http://saudi4cam.com				
http://voice.ksacam.com
http://سعودي-كام.oo2o.com
http://سعوديكام.com
http://xn--mgbply5cnrr.com
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/
http://www.dir.3rabstarz.com/
http://www.up.3rabstarz.com
http://www.3rabstarz.com/vb/forumdisplay.php?f=5
http://www.3rabstarz.com/vb/forumdisplay.php?f=116
http://www.3rabstarz.com/vb/forumdisplay.php?f=66
http://www.3rabstarz.com/vb/forumdisplay.php?f=78
http://www.3rabstarz.com/vb/forumdisplay.php?f=147
http://www.3rabstarz.com/vb/forumdisplay.php?f=2
http://www.3rabstarz.com/vb/forumdisplay.php?f=31
http://www.3rabstarz.com/vb/forumdisplay.php?f=136
http://www.3rabstarz.com/vb/forumdisplay.php?f=34
http://www.3rabstarz.com/vb/forumdisplay.php?f=89
http://www.3rabstarz.com/vb/forumdisplay.php?f=26
http://www.3rabstarz.com/vb/forumdisplay.php?f=72
http://www.3rabstarz.com/vb/forumdisplay.php?f=73
http://www.3rabstarz.com/vb/forumdisplay.php?f=149
http://www.3rabstarz.com/vb/forumdisplay.php?f=150
http://www.3rabstarz.com/vb/forumdisplay.php?f=151
http://www.3rabstarz.com/vb/forumdisplay.php?f=152
http://www.3rabstarz.com/vb/forumdisplay.php?f=67
http://www.3rabstarz.com/vb/forumdisplay.php?f=3
http://www.3rabstarz.com/vb/forumdisplay.php?f=37
http://www.3rabstarz.com/vb/forumdisplay.php?f=115
http://www.3rabstarz.com/vb/forumdisplay.php?f=76
http://www.3rabstarz.com/vb/forumdisplay.php?f=77
http://www.3rabstarz.com/vb/forumdisplay.php?f=36
http://www.3rabstarz.com/vb/forumdisplay.php?f=153
http://www.3rabstarz.com/vb/forumdisplay.php?f=122
http://www.3rabstarz.com/vb/forumdisplay.php?f=118
http://www.3rabstarz.com/vb/forumdisplay.php?f=119
http://www.3rabstarz.com/vb/forumdisplay.php?f=120
http://www.3rabstarz.com/vb/forumdisplay.php?f=121
http://www.3rabstarz.com/vb/forumdisplay.php?f=35
http://www.3rabstarz.com/vb/forumdisplay.php?f=65
http://www.3rabstarz.com/vb/forumdisplay.php?f=7
http://www.3rabstarz.com/vb/forumdisplay.php?f=9
http://www.3rabstarz.com/vb/forumdisplay.php?f=11
http://www.3rabstarz.com/vb/forumdisplay.php?f=142
http://www.3rabstarz.com/vb/forumdisplay.php?f=143
http://www.3rabstarz.com/vb/forumdisplay.php?f=146
http://www.3rabstarz.com/vb/forumdisplay.php?f=140
http://www.3rabstarz.com/vb/forumdisplay.php?f=137
http://www.3rabstarz.com/vb/forumdisplay.php?f=114
http://www.3rabstarz.com/vb/forumdisplay.php?f=117
http://www.3rabstarz.com/vb/forumdisplay.php?f=113
http://www.3rabstarz.com/vb/forumdisplay.php?f=51
http://www.3rabstarz.com/vb/forumdisplay.php?f=84
http://www.3rabstarz.com/vb/forumdisplay.php?f=138
http://www.3rabstarz.com/vb/forumdisplay.php?f=139
http://www.3rabstarz.com/vb/forumdisplay.php?f=15
http://www.3rabstarz.com/vb/forumdisplay.php?f=130
http://www.3rabstarz.com/vb/forumdisplay.php?f=97
http://www.3rabstarz.com/vb/forumdisplay.php?f=102
http://www.3rabstarz.com/vb/forumdisplay.php?f=101
http://www.3rabstarz.com/vb/forumdisplay.php?f=104
http://www.3rabstarz.com/vb/forumdisplay.php?f=131
http://www.3rabstarz.com/vb/forumdisplay.php?f=103
http://www.3rabstarz.com/vb/forumdisplay.php?f=99
http://www.3rabstarz.com/vb/forumdisplay.php?f=59
http://www.3rabstarz.com/vb/forumdisplay.php?f=60
http://www.3rabstarz.com/vb/forumdisplay.php?f=39
http://www.3rabstarz.com/vb/forumdisplay.php?f=40
http://www.3rabstarz.com/vb/forumdisplay.php?f=42
http://www.3rabstarz.com/vb/forumdisplay.php?f=4
http://www.3rabstarz.com/vb/forumdisplay.php?f=1
http://www.3rabstarz.com/vb/forumdisplay.php?f=88
http://www.3rabstarz.com/vb/forumdisplay.php?f=148
http://www.3rabstarz.com/vb/forumdisplay.php?f=62
http://www.3rabstarz.com/vb/forumdisplay.php?f=63
http://www.3rabstarz.com/vb/forumdisplay.php?f=64
http://www.3rabstarz.com/vb/forumdisplay.php?f=6
http://www.3rabstarz.com/vb/forumdisplay.php?f=12
http://www.3rabstarz.com/vb/forumdisplay.php?f=96
http://www.3rabstarz.com/vb/forumdisplay.php?f=58
http://www.3rabstarz.com/vb/forumdisplay.php?f=38