from bs4 import BeautifulSoup import urllib2 import csv import datetime import sys if len(sys.argv[1:]) == 0: print "No supplied URLs" sys.exit() gameIDs = sys.argv[1:] # [TeamAway, Win/Loss, GF, GA, ShFor, ShA] # [TeamHome, Loss/Win, GA, GF, ShFor, ShA] now = datetime.datetime.now() fileName = "/Users/joshuaweissbock/Dropbox/CSI-5388/Project/dailyscores/"+str(now.day)+"-"+str(now.month)+"-"+str(now.year)+".csv" myfile = open(fileName, 'wb') wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) # loop through all gameIDs for g in gameIDs: fenwickURL = "http://www.tsn.ca"+g request = urllib2.Request(fenwickURL) response = urllib2.urlopen(request) the_page = response.read() soup = BeautifulSoup(the_page) box = soup.findAll('table', 'boxScore') # store the tables col = len(box[0].findAll('tr')[1].findAll('td')) # count if it is OT or not #print col TeamAway = box[0].findAll('tr')[1].findAll('td')[0].text # get the home/away team name TeamHome = box[0].findAll('tr')[2].findAll('td')[0].text GF = box[0].findAll('tr')[1].findAll('td')[col-1].text # get the teams scores GA = box[0].findAll('tr')[2].findAll('td')[col-1].text statusAway = "Win" if GF > GA else "Loss" # determine who won statusHome = "Loss" if statusAway == "Win" else "Win" try: isSO = box[0].findAll('tr')[0].findAll('th')[5].text sub = 2 if isSO == 'SO' else 1 except: sub = 1 ShFor = box[1].findAll('tr')[1].findAll('td')[col-sub].text # get shots for ShA = box[1].findAll('tr')[2].findAll('td')[col-sub].text Line1 = [TeamAway, statusAway, GF, GA, ShFor, ShA] # store them in the right format Line2 = [TeamHome, statusHome, GA, GF, ShA, ShFor] # output the lines print Line1 print Line2 # write to csv wr.writerow(Line1) wr.writerow(Line2)