Write a python script to print the latest share value of a company whose NASDAQ symbol would be given as command line argument.
This script can be run by these commands:
$ python sharevalue.py [Nasdaq Symbol]
or, if made executable,
$ ./sharevalue.py [Nasdaq Symbol]
#!/usr/bin/env python import urllib2 # the module is imported for opening url import sys # sys module is imported for command line arguments def share(symbol): # The function is defined for opening and reading the url, and for printing sharevalue try: # Used for handle exception and error may be due to problems like url not opening b = urllib2.urlopen('http://download.finance.yahoo.com/d/quotes.csv?s='+symbol+'&f=l1') # opens the url a = float(b.read()) # Reads the url if a == 0.00: # if any wrong symbol is entered then it print the sharevalue zero , which is not right, so removing it by if-else statement print "The Nasdaq code entered is wrong" else: print "The current sharevalue for the given NASDAQ symbol is %f" % (a) except: print "failed to open the finance.yahoo.com url" if __name__ == '__main__': if len(sys.argv) !=2: print "Incorrect entry. Enter a NASDAQ symbol" sys.exit(1) else: share(sys.argv[1]) sys.exit(1)
~
Despite considerable time, this project did not have good results.
I don't know where I went wrong. I followed the instructions from the assigned reading material. Things appeared to work, but the actual planetparser.py file never seemed to be present when the tarball was untarred and python setup.py install was ran (and no errors were returned in any part of the process of creation or installation).
In order to avoid turning in nothing, I did create a script that does return the title and author of the the current postings on the Fedora blog (in python of course) without any additional modules needing to be installed. It all worked testing via ssh to another computer of mine. But, I am sure my solution will probably be considered cheating (errr somthing).
#!/usr/bin/env python import urllib2 import sys # get data from "The Fedora Project" blog site and set to list called data url_for_fedora_blog = "http://planet.fedoraproject.org/rss10.xml" data = urllib2.urlopen(url_for_fedora_blog).readlines() # make new list called bloglist, with end characters stripped off bloglist = [a.rstrip() for a in data] # make another new list called bloglist_title reducing data to just blog entries bloglist_title = [] for x in bloglist: if x.endswith('title>') == True: bloglist_title.append(str(x)) # make another new list called bloglist_tab stripping off any leading tab spaces bloglist_tab = [] for y in bloglist_title: y = y.strip(' \t\n\r') bloglist_tab.append(str(y)) # stil more stripping of the strings, this time the leading html tags bloglist_html_left = [] for z in bloglist_tab: z = z.lstrip("<title>") bloglist_html_left.append(str(z)) # another stripping function to remove trailing tags bloglist_html_right = [] for i in bloglist_html_left: i = i[:-8] bloglist_html_right.append(str(i)) # finally the final list splitting and reversing the strings # printing the final strings with the colon removed and BY added where split bloglist_newline = [] for j in bloglist_html_right: k = j.split(": ") k.reverse() l = " BY ".join(k) print l
To write a script that prints the title and the author of every blog posted in http://planet.fedoraproject.org, making use of the virtualenv feature.
As I had already installed python-virtualenv, I've jumpted directly to create a new virual environment:
$mkdir virtual $cd virtual $virtualenv virtual_planetparse The PYTHONDONTWRITEBYTECODE environment variable is not compatible with setuptools. Either use --distribute or unset PYTHONDONTWRITEBYTECODE.
First problem arises. Setting PYTHONDONTWRITEBYTECODE="" works it around
$PYTHONDONTWRITEBYTECODE="" virtualenv virtual_planetparse New python executable in virtual_planetparse/bin/python Installing setuptools............done. Installing pip...............done. $ source virtual_planetparse/bin/activate (virtual_planetparse) $ pip install beautifulsoup4 [...] Successfully installed beautifulsoup4 Cleaning up... (virtual_planetparse) $ pip install html5lib Successfully installed html5lib Cleaning up...
It works as explained in the comments. it gets the html of the site with urllub2.urlopen(). Then it parses it using BeautifulSoup, and select(). the syntax used to select the desired tags is the following:
".blog-entry-author > a" # The tag "a" (link) inside the tag of class (note the point meaning class) "blog-entry-author"
This is the whole code:
1 #!/usr/bin/env python 2 3 # Assignment: Get the titles and authors of all the blogs feeded 4 # at http://planet.fedoraproject.org. 5 # 6 # Student: Josep Caselles 7 # Course: #dgplug Summer Training Course 8 # Date: 14/07/2013 9 10 from sys import exit 11 from urllib2 import urlopen 12 from bs4 import BeautifulSoup 13 14 URL_CONSTANT = "http://planet.fedoraproject.org" 15 16 def print_blog_info (): 17 18 """ 19 This method will use BeautifulSoup to parse the content of the given url 20 and extract from it the desired content. With select() method from 21 BeautifulSoup you can get all tags given it's class, id, or any other 22 attribute. for a complete reference, see http://tinyurl.com/nn4m7hg. 23 24 Steps made: 25 1- Fetch the whole html with urllib2 urlopen() 26 2- "Soupe" it with BeautifulSoup 27 3- Select the desired tag's content 28 4- print accordingly 29 30 """ 31 32 try: 33 html_doc = urlopen (URL_CONSTANT) 34 35 except: 36 exit("\nError: Something is wrong with http://planet.fedoraproject.org" 37 " or your internet connection\n") 38 39 html_souped = BeautifulSoup (html_doc) 40 html_doc.close() 41 42 z = 0 43 44 for x, y in zip(html_souped.select(".blog-entry-author > a"), 45 html_souped.select(".blog-entry-title > a")): 46 47 z += 1 48 49 print """ 50 Blog Entry n. %.2i: 51 ----------------- 52 53 Tile: '%s' 54 Author: %s 55 """ % (z, y.string, x.string) 56 57 58 if __name__ == "__main__": 59 print_blog_info () 60 exit(0)
I installed beautifulsoup4 and lxml modules for this assignment in my 'virt1' environment.
(virt1) $ yolk -l Python - 2.7.5 - active development (/usr/lib/python2.7/lib-dynload) beautifulsoup4 - 4.2.1 - active lxml - 3.2.1 - active pip - 1.3.1 - active setuptools - 0.6c11 - active wsgiref - 0.1.2 - active development (/usr/lib/python2.7) yolk - 0.4.3 - active
This program will read a web page and output blog title and author.
$ python planetparser.py
A link to the source code.
Renich Bon Ciric HowTo: Bind10 resolver @ Fedora 19 Fedora Indonesia FAD Klaten 2013; Menuju Klaten Go Online Tom 'spot' Callaway In Memory of Seth Vidal
In the main function, retrieve data from URL and store them into a string.
# fetch data s_url = 'http://planet.fedoraproject.org' try: f = urllib2.urlopen(s_url) except urllib2.URLError: print 'failed to open url', s_url else: html_doc = f.read() f.close() #print html_doc
Using following filter conditions to retrieve blog title & author
# extract title & author tags_header = SoupStrainer(class_="blog-entry-header") soup = BeautifulSoup(html_doc, "lxml", parse_only=tags_header) #soup = BeautifulSoup(html_doc, "html.parser", parse_only=tags_header) #print soup for tag in soup.find_all('a', href=re.compile("http:")): print tag.string
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.
$ python sharevalue.py GOOG YHOO GOOG YHOO 920.24 27.04
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)
import urllib2 import sys """Function is used to get the quoted price of the NASDAQ symbol,which is entered as command line argument.""" def price(sym): quote= urllib2.urlopen('http://download.finance.yahoo.com/d/quotes.csv?s='+sym+'&f=l1') print "This is the latest quote for the given NASDAQ symbol is "+quote.read() """Here is the main function where the argument passed in the commandline is checked to see whether it is a valid input or not and then passed to the function price().""" if __name__ == '__main__': if len(sys.argv) !=2: print "Incorrect entry. Enter a NASDAQ symbol" sys.exit(1) else: price(sys.argv[1]) sys.exit(1)
Link to the code is https://github.com/puspita23/puspita_utility/blob/master/sharevalue/sharevalue.py
The comments in the code explain it profusely. In short, I've taken it as an oportunity to exercise not just the use of modules and parsing content from a website, but also the use of classes and inheritance. I've defined a parent class named data_retriever, which works as a dummy base, it just declare a container variable and defines a method that returns it. It doesn't have any real use in this case, but seems reasonable in another context, where this script could serve as a module for all kinds of data retrievement.
A child of data_retriever is from_url_retriever, which with it's proper elaboration shoud serve for retrieving from web sites in general, provided it's url. It's a more concret application of data_retriever, and that's the essence of hineritance.
Finally, it's defined a child of from_url_retriever, which is called yahoo_share_retriever. As from_url_retriever is a child of data_retriever, yahoo_share_retriever also inherits from data_retriever. This class, it's another concretion of useness, focused on a concret content of a concret web, This child, acts as a helper for printing the content of the preestablished website we plan to use regularly in our code (i.e. http://download.finance.yahoo.com)
1 #!/usr/bin/env python 2 3 # Assignment: Retrieve from http://download.finance.yahoo.com the share value 4 # of a given NASDAQ company's symbol. 5 # 6 #To achieve this, we need the urllib2 module. 7 # 8 9 import sys 10 import urllib2 11 12 13 class data_retriever (object): 14 15 """ 16 This class will serve as base class for retrieving content from urls. 17 Splitting this in two classes may be dumb, but it will serve as 18 an hineritance exercise as well. 19 20 """ 21 22 def __init__ (self): 23 self.content = None # Content is where all data will be stored 24 25 def return_data (): 26 27 """ 28 Will retrieve the data container, only if it's populated. 29 30 """ 31 32 if self.content: 33 return self.content 34 else: 35 print "Error. Data not retrieved properly" 36 37 38 class from_url_retriever (data_retriever): 39 """ 40 This class is a child of data_retriever. Inherits from it the variable 41 content, which serves as container for all the data, and the method 42 return_data, which serves for returning this data container. 43 44 This class adds the functionality to retrieve data from an url, using 45 urllib2 module, and defines the methods retrieve_from_url, which returns 46 an open file contining the content of the Url, and print_content, which 47 prints the whole content of the given Url. 48 49 """ 50 51 52 def __init__ (self, url): 53 data_retriever.__init__ (self) 54 self.url = url 55 56 57 def retrieve_from_url (self, url): 58 59 """ 60 This method uses the function urlopen from urllib2 module and 61 retrieves all content of the site to url_content, which is 62 a kind of file-object, already open 63 64 This method handles the URLError and ValueError exceptions, and exits 65 with an error message 66 67 """ 68 69 try: 70 url_content = urllib2.urlopen(url) 71 72 except urllib2.URLError: 73 error_message = "Your url <" + url + "> is wrong, please fix" 74 sys.exit(error_message) 75 76 except ValueError: 77 error_message = "Your url <" + url + "> has an unknown url" \ 78 " type. Please fix" 79 sys.exit(error_message) 80 81 else: 82 return url_content 83 84 85 def store_content (self): 86 87 """ 88 This method calls for retrieve_from_url method and stores its return 89 in content. 90 91 """ 92 93 self.open_file_content = self.retrieve_from_url (self.url) 94 self.content = self.open_file_content.read() 95 self.open_file_content.close() # The return of urllib2.urlopen() is a 96 # kind of file, closing it for safety 97 98 99 def print_content (self): 100 101 """ 102 Calls for store_content() and prints content if not empty. 103 104 """ 105 106 self.store_content() 107 if self.content: 108 print self.content 109 else: 110 error_message = "Something went wrong. Fix it" 111 sys.exit(error_message) 112 113 114 class yahoo_share_retriever (from_url_retriever): 115 116 """ 117 This class is a child of from_url_retriever, which sets the link 118 for yahoo share value download service, and asks for it's argument 119 only the company's symbol. Instead of asking for the url as 120 from_url_retriever, it uses the download.finance.com service of Yahoo. 121 122 In order to choose which company you want to search, we hardcode the 123 link splitted in 2 halves, and we keep it in a list. once an instance 124 is created, the constructor asks for nasdaq_symbol, which we insert in the 125 middle of the 2 halves (position 1 in url list) and then we join the list 126 into one single string,which we then pass to from_url_retriever constructor. 127 128 Using this class, everything is prepared for be able to print just by 129 creating an instance and calling print_content(). print_content overrides 130 print_content from the parent class, to strip the content which is provided 131 with '\r\n', and print an aproppiate message 132 133 """ 134 135 def __init__ (self, nasdaq_symbol): 136 137 if nasdaq_symbol.isalpha(): # Risky, dont' know if they are only 138 # only alphabets, but anyway... 139 140 url = ["http://download.finance.yahoo.com" 141 "/d/quotes.csv?s=", "&f=l1"] 142 url.insert(1, nasdaq_symbol) 143 url = "".join(url) 144 145 else: 146 error_message = nasdaq_symbol + " is not a correct NASDAQ symbol" 147 sys.exit(error_message) 148 149 from_url_retriever.__init__ (self, url) 150 self.nasdaq_symbol = nasdaq_symbol.upper() # We keep it for output 151 152 153 def print_content(self): 154 155 """ 156 Overriden method that strips content to adapt the concret content of 157 the Yahoo service, and prints an aproppiate message. 158 159 Yahoo service will generate a cvs file even if the symbol is incorrect. 160 It will contain 0.00, which is not good. This code makes sure it's 161 a desired output, or tells the user it's a wrong symbol 162 163 """ 164 165 self.store_content() 166 self.content = self.content.rstrip("\r\n") 167 168 if self.content.find("0.00") >= 0: 169 error_message = "The NASDAQ symbol '" + self.nasdaq_symbol +"' is" \ 170 " incorrect. Please make sure you provide a" \ 171 " correct symbol." 172 sys.exit(error_message) 173 174 else: 175 print "Share value for %s: %s" % (self.nasdaq_symbol, self.content) 176 177 178 if __name__ == "__main__": 179 180 if len(sys.argv) == 2: 181 yahoo_share_retriever (sys.argv[1]).print_content() 182 sys.exit(0) 183 184 else: 185 sys.exit("Usage: ./sharevalue.py <NASDAQ_SYMBOL>")
Writing a python script to get the value of the market share of a specific NASDAQ from Yahoo.
I divided the code into two files. The main code in sharevalue.py and the Yahoo API module in YahooAPI.py. This way the API module can be used again with a different python script and the main code can use different API modules.
""" Yahoo API Module ================ YahooFinance Class ------------------ This class will save the ticker in a correct link format and will output the correct value when value is called """ import urllib2 class YFinance(object): """ The YahooAPI has a correct link and the value returned from Yahoo Finance """ def __init__(self): """ The default link is None """ self._link = None @property def link(self): """ This will return the link when needed to access the website and get the value """ return self._link @property def value(self): """ We try to connect to the link if everything goes well, we get the value we check if the value is not 0 and return it if we can't connect we will return -1 """ try: fobj = urllib2.urlopen(self._link) data = fobj.read() value = float(data) fobj.close() if value == 0.0: return return value except IOError: return -1 @link.setter def link(self, ticker): """ The link recieves a ticker and saves it as a link The link will be used later to get the information from the web page. """ link = "http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=l1" \ % ticker self._link = link
Problem: To generate a python program to print the share value of a company
Description
Click the link below to see the program
Steps to execute the program
$ chmod +x sharevalue.py
$ ./sharevalue.py GOOG
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]