m0rin09ma3 planetparser 20130714-163119


Posted:

This program will print the last traded price of the company whose symbol is given to the program as command-line arguments like:

$ python sharevalue.py GOOG YHOO ...

A link to the source code.

Sample outputs:

$ python sharevalue.py GOOG YHOO
GOOG    YHOO
920.24  27.04

Explanation

In the main function, take command-line arguments and construct url string.

n_argc = len(sys.argv)
if n_argc < 2:
    print 'please specify symbol and try again.'
    print 'e.g. sharevalue.py GOOG YHOO ...'
    return 1
#print n_argc

l_symbol = []
l_symbol = [ sys.argv[n] for n in range(1, n_argc) ]
#print l_symbol

# s_symbol = 's=GOOG&s=YHOO&...'
s_symbol = ""
s_symbol = '&'.join( [ 's=' + l_symbol[n] for n in range(n_argc - 1) ] )
#print s_symbol

s_url = ""
s_url = 'http://download.finance.yahoo.com/d/quotes.csv?' + \
s_symbol + \
'&f=l1'
#print s_url

Retrieve data from the URL and store them into a list.

try:
    f = urllib2.urlopen(s_url)
except urllib2.URLError:
    print 'failed to open url', s_url
else:
    l_quote = []
    l_quote = f.readlines() # e.g. ['920.24\r\n', '27.04\r\n']
    f.close()
#print l_quote

Format strings and output.

print "%s".ljust(6) % '\t'.join(l_symbol)
l_quote = [ s.strip() for s in l_quote ]
print "%6s" % '\t'.join(l_quote)
Contents © 2013 dgplug - Powered by Nikola
Share
UA-42392315-1