I'm having trouble finishing this program. The goal of the program is to read a file that contains the abbreviation of a stock and a number that stands for the amount of shares. I am then going to put the full company name, price of the stock, number of shares, and total investments in an html file in a table. I am having trouble separating the abbreviation of the company from the number of shares in the text file. This is what I have so far:
from string import *
import urllib2
class getURLinfo:
def __init__( self ):
"""Constructor, creates the object and assigns "None" to the
instance variables."""
self.url = None
self.begMarker = None
self.endMarker = None
def setURL( self, u ):
"""Mutator: assign value to the instance variable, URL"""
self.url = u
def setMarkers( self, begin, end ):
"""Mutator: assign values to the instance variables begMarker
and endMarker"""
self.begMarker = begin
self.endMarker = end
def getInfo( self, identifier ):
"""Return the text between the two markers for the specific URL
indicated by the 'identifier' parameter."""
# Create the exact URL string and retrieve the indicated html text
f = urllib2.urlopen( self.url % identifier )
html = f.read()
# Locate the markers in the htmlText string
begIndex = html.find( self.begMarker )
endIndex = html.find( self.endMarker, begIndex )
# Include some simple error checking if the markers are not found
if begIndex== -1 or endIndex== -1:
return None
# Advance the starting index to the actual beginning of the desired text
begIndex = begIndex + len( self.begMarker )
# Slice out and return the desired text
return html[ begIndex : endIndex ]
# ---------------------------------------------------------
# Testing Area
# ---------------------------------------------------------
"""
if __name__=="__main__":
# Create an object of type getURLinfo
zodSite = getURLinfo()
# Initialize the instance variables
zodSite.setURL("http://my.horoscope.com/astrology/free-daily-horoscope-%s.html")
zodSite.setMarkers('id="textline">',"</div>")
# Retrieve the horoscope and display it
horos = zodSite.getInfo("capricorn")
print "Your horoscope is: \n",horos
"""
"""
Class definition for a list of stocks.
Instance variables: name and price
"""
class STOCKINFO:
def __init__(self):
#This is the constructor for class FriendInfo
self.abbrev = None
self.share = None
self.name = None
self.price = None
def otherInfo(self, ab, sh):
self.abbrev = ab
self.share = sh
def newInfo(self, nm, pr):
#Enters the stock information
self.name = nm
self.price = pr
def updateAbbrev(self, newAb):
self.abbrev = newAb
def updateShare(self, newSh):
self.share = newSh
def updateName(self, newNm):
self.name = newNm
def updatePrice(self, newPr):
self.price = newPr
def getAbbrev(self):
return self.abbrev
def getShare(self):
return self.share
def getName(self):
return self.name
def getPrice(self):
return self.price
def showInfo(self):
print "This stock's info is:\n\t%s\n\t%s\n\t%s\n\t%s" \
% (self.name, self.price)
#---------------------------------------------------------
# Testing Area
"""
if __name__ == "__main__":
print "\n\nCreating new information entries..."
f1 = STOCKINFO()
f2 = STOCKINFO()
print "\nFilling in data"
f1.newInfo('GOOG', '75', 'Google', '544.26')
f2.newInfo('GM', '12','General Motors', '32.09')
print "\nPrinting information in datatbase"
f1.showInfo()
f2.showInfo()
print "\nUpdating the information"
f1.updateAbbrev('GE')
f2.updateShare('500')
f1.updateName('Google')
f2.updatePrice('544.10')
print "\nShowing the updated info \n"
f1.showInfo()
f2.showInfo()
print
print "Using the getXX() methods..."
print "getAbbrev = ", f1.getAbbrev()
print "getShare = ", f1.getShare()
print "getName = ", f1.getName()
print "getPrice = ", f1.getPrice()
"""
def openHTML():
"""
This function creates and opens the HTML called "stock.html"
and returns the variable file1 at the end.
"""
file1 = open("stock.html", 'w')
file1.write("""\n<center><FONT COLOR="#00FF00"><html>\n<head><title>Stock \
Portfolio</title></head>""")
return file1
def Title(file1):
"""
This function creates a title on the webpage.
"""
file1.write("""<body bgcolor="#151B8D">
<h1>Stock Portfolio</h1>
<p>My total portfolio value is: </p>""")
def closeHTML(file1):
"""
This function will close the html file.
"""
file1.close()
def getStocks():
file2= open("stock.txt", 'r')
slist = []
for word in file2:
stock = word[:-1]
slist.append(stock)
print "The stocks you have are:"
print slist
return slist, file2
def getStocksURL(slist):
stockInfo = STOCKINFO()
sURL = getURLinfo()
sURL.setURL("http://finance.yahoo.com/q?s=%s")
sURL.setMarkers("""</b></span> </div><h1>""", "<span>")
name = sURL.getInfo(slist)
print "Company name: ", name
sURL.setMarkers("""</small><big><b><span id="yfs_l10_""", "</span></b>")
price = sURL.getInfo(slist)
print "Stock price is: ", price
stockInfo.newInfo(name, price)
return stockInfo
def stockInformation(slist):
stocklist = []
for stock2 in slist:
stockInfo = getStocksURL(stock2)
stocklist.append(stockInfo)
#print stocklist
return stocklist
def createTable (file1, Stocks):
file1.write("""<p>
<table border="1">
<tr>
<td>Company Name</td>
<td>Stock Price</td>
<td>Number of Shares</td>
<td>Total Investment</td>
</tr>""")
for stock in Stocks:
file1.write("""<tr>
<td>""" + str (stock.getAbbrev()) +"""</td>
<td>""" + str (stock.getShare()) + """</td>
<td>""" + str (stock.getName()) + """</td>
<td>""" + str (stock.getPrice()) + """</td>""")
def main():
f = openHTML()
Title(f)
slist = getStocks()
stocks = stockInformation(slist)
createTable(f, stocks)
closeHTML(f)
main()
Thank you!!