The assignment was to print share value of a given company NASDAQ symbol.
This script can take many NASDAQ symbol at one go & will give desired output.
It is able to print the corresponding company name (if any) from given NASDAQ symbol.
This program will print the current share price of the company whose NASDAQ value is given to the program as an argument like:
./sharevalue.py [NASDAQ symbol] [NASDAQ symbol] [NASDAQ symbol]
1 #!/usr/bin/env python 2 3 import urllib2 4 import sys 5 6 def main(nasdaq): 7 8 """ 9 Here all work is being done. 10 11 :arg nasdaq: NASDAQ symbol 12 """ 13 14 #Making the link in 'link' string to fetch share value 15 link = 'http://download.finance.yahoo.com/d/quotes.csv?s='+nasdaq+'&f=l1' 16 print 'Fetching recent share value of '+nasdaq 17 18 #Share value saved into an instance share_value 19 share_value = urllib2.urlopen(link) 20 21 #Fetching data to find company name 22 html_data=urllib2.urlopen('http://www.nasdaq.com/symbol/'+nasdaq) 23 name=html_data.read() 24 25 #Finding the Company name & Displaying 26 start=name.find('<title>')+7 27 end=name.find('</title>')-25 28 while start < end: 29 print name[start], 30 start+=1 31 html_data.close() 32 33 #Printing share_value which was saved in share_value previously 34 print '\n'+'Current share price of company '+nasdaq+': '+share_value.read() 35 share_value.close() 36 37 if __name__ == '__main__': 38 39 #Counting the NASDAQ Symbol (Arguments) 40 count = len(sys.argv) - 1 41 42 #Checking if atleast one symbol available or not 43 if count >= 1: 44 45 #NASDAQ symbol available passing it to main() function 46 i=0 47 while i < count: 48 i+=1 49 main(sys.argv[i]) 50 51 else: 52 #If nothing given as argument it will print the syntax to use this code" 53 print 'Atleast 1 Argument needed'+'\n'+'Syntax: ./sharevalue.py [NASDAQ Symbol] [NASDAQ Symbol]' 54 sys.exit(0)
Run the above script like:
$ ./sharevalue.py [NASDAQ value] [NASDAQ symbol] [NASDAQ symbol] [NASDAQ symbol]
or:
$ python sharevalue.py [NASDAQ value] [NASDAQ symbol] [NASDAQ symbol] [NASDAQ symbol]
Here some example output is given below:
$ ./sharevalue.py ORCL Fetching recent share value of ORCL O R C L s t o c k q u o t e - O r a c l e C o r p o r a t i o n Current share price of company ORCL: 31.86 $ ./sharevalue.py ORCL GOOG RHT Fetching recent share value of ORCL O R C L s t o c k q u o t e - O r a c l e C o r p o r a t i o n Current share price of company ORCL: 31.86 Fetching recent share value of GOOG G O O G s t o c k q u o t e - G o o g l e I n c . Current share price of company GOOG: 920.24 Fetching recent share value of RHT R H T s t o c k q u o t e - R e d H a t , I n c . Current share price of company RHT: 50.57 $./sharevalue.py Atleast 1 Argument needed Syntax: ./sharevalue.py [NASDAQ Symbol] [NASDAQ Symbol]