Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- from BeautifulSoup import BeautifulSoup
- import numpy as NP
- import sys
- def parseSpreads(spreads):
- for spread in spreads:
- try:
- spreadValue = spread.renderContents().replace('½', '.5')
- yield float(spreadValue)
- except Exception, e:
- pass
- def extractOdds(soup):
- games = {}
- for cell in soup.findAll('td', attrs={'class': 'teams'}):
- teams = cell.findAll('span', attrs={'class': 'team'})
- team1, team2 = [str(t.find('a').renderContents()) for t in teams]
- games[(team1, team2)] = {}
- topspreads = [c for c in cell.parent.findAll('span', 'top-line')]
- botspreads = [c for c in cell.parent.findAll('span', 'bottom-line')]
- games[(team1,team2)]['topSpreads'] = [ts for ts in parseSpreads(topspreads)]
- games[(team1,team2)]['botSpreads'] = [ts for ts in parseSpreads(botspreads)]
- if games[(team1,team2)]['topSpreads']:
- games[(team1,team2)]['avgSpread'] = NP.average(games[(team1,team2)]['topSpreads'])
- elif games[(team1,team2)]['botSpreads']:
- games[(team1,team2)]['avgSpread'] = NP.average(games[(team1,team2)]['botSpreads'])
- else:
- games[(team1,team2)]['avgSpread'] = 0.0
- overunders = [c.find('span') for c in cell.parent.findAll('div', 'over-under')]
- games[(team1,team2)]['overUnders'] = [ou for ou in parseSpreads(overunders)]
- games[(team1,team2)]['avgOverUnder'] = NP.average(games[(team1,team2)]['overUnders'])
- return games
- def main():
- filename = "html/odds_20090918.html"
- file = open(filename)
- soup = BeautifulSoup(''.join(file.readlines()))
- file.close()
- games = extractOdds(soup)
- print "%7s | %-25s %-25s %6s %6s" % ('weight', 'winner', 'loser', 'spread', 'o/u')
- print "================================================================================"
- for i, ((team1,team2),data) in enumerate(sorted(games.items(), key=lambda (k,v): (v['avgSpread'], -1 * v['avgOverUnder']))):
- weight = len(games) - i
- if data['topSpreads']:
- winner,loser = team1,team2
- else:
- winner,loser = team2,team1
- print "%7d | %-25s over %-25s %6.2f %6.2f" % (weight, winner, loser, data['avgSpread'], data['avgOverUnder'])
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement