.. link: http://dgplug.org/summertraining/2013/posts/m0rin09ma3-planetparser-20130714-163119.html .. description: .. tags: .. date: 2013/07/14 16:31:20 .. title: m0rin09ma3 planetparser 20130714-163119 .. slug: m0rin09ma3-planetparser-20130714-163119 Sharevalue =========== 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`_. .. _source code: https://github.com/m0rin09ma3/python-summer-training-2013/blob/master/sharevalue/sharevalue.py Sample outputs: ---------------- .. code-block:: $ python sharevalue.py GOOG YHOO GOOG YHOO 920.24 27.04 Explanation ------------ In the main function, take command-line arguments and construct url string. .. code-block:: python 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. .. code-block:: python 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. .. code-block:: python print "%s".ljust(6) % '\t'.join(l_symbol) l_quote = [ s.strip() for s in l_quote ] print "%6s" % '\t'.join(l_quote)