#This displays the prices of the 30 stocks in the #Dow Jones Industrial Average and the Dow Jones Average #itself. def DowJones(): #The thirty stocks currently making up the Dow Jones Industrial Average components=[["3M","MMM"],["Alcoa","AA"],["American Express","AXP"], ["AT&T","T"],["Bank of America","BAC"],["Boeing","BA"], ["Caterpillar","CA"],["Chevron Corporation","CVX"], ["Citigroup","C"],["Coca-Cola","KO"],["DuPont","DD"],["ExxonMobil","XOM"], ["General Electric","GE"],["General Motors","GM"],["Hewlett-Packard","HPQ"], ["Home Depot","HD"],["Intel","INTC"],["IBM","IBM"],["Johnson and Johnson","JNJ"], ["JPMorgan Chase","JPM"],["Kraft Foods","KFT"],["McDonald's","MCD"],["Merck","MRK"], ["Microsoft","MSFT"],["Pfizer","PFE"],["Procter & Gamble","PG"], ["United Technologies Corporation","UTX"],["Verizon Communications","VZ"], ["Walmart","WMT"],["Walt Disney","DIS"]] dowDivisor=0.125552709 prices=[0.0]*30 sum=0.0 for j in range(0,30): result=getStockPrice2(components[j][1]) prices[j]=float(result[1]) sum=sum+prices[j] print "DOW JONES INDUSTRIAL AVERAGE\n\n\n" print "Time: "+result[0]+"\n\n" for j in range(0,30): print components[j][0],"\t\t\t",prices[j] print "\n\n" print "Average:\t",round(100*(sum/dowDivisor))/100 from urllib import * def getStockPrice2(stock_code): webstream=urlopen("http://finance.yahoo.com/q?s="+stock_code) results=webstream.readlines() webstream.close() #if there's an invalid stock identifier, #then line 92 of the page contains error information if len(results)>92 and results[92].find("There are no") != -1: result = 0 #otherwise, line 9 contains the price and time of day, #which we now proceed to extract from the string else: priceline=results[9] loc1=priceline.find("yfs_t10") loc2=priceline.find(">",loc1) loc3=priceline.find("<",loc2) loc4=priceline.find("yfs_l10") loc5=priceline.find(">",loc4) loc6=priceline.find("<",loc5) if -1 in [loc1,loc2,loc3,loc4,loc5,loc6]: #if this happens, then the website's format must #have changed result=-1 else: time=priceline[loc2+1:loc3] price=priceline[loc5+1:loc6] result=[time,price] return result