Created
September 5, 2017 23:55
-
-
Save brandonkrull/0625694b32d422d0736fc2daace8db47 to your computer and use it in GitHub Desktop.
Get ESPN's Matchup Predictions for 2017 NFL Seasons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Hacky parse through ESPN's NFL schedule page. | |
""" | |
import pickle | |
import requests | |
from collections import namedtuple | |
from bs4 import BeautifulSoup as bs | |
base_url = 'http://www.espn.com' | |
Stats = namedtuple('Stats', ['home', 'home_val', 'away', 'away_val', 'score']) | |
FPI = { # 2017 FPI Rankings | |
'NE': 9.2, 'GB': 4.8, 'PIT': 4.7, 'SEA': 4.2, 'ATL': 3.8, 'DAL': 3.8, | |
'OAK': 3.7, 'KC': 3.4, 'CAR': 1.8, 'BAL': 1.3, 'NYG': 1.2, 'DEN': 0.8, | |
'MIN': 0.8, 'CIN': 0.3, 'PHI': 0.2, 'TB': 0.1, 'NO': 0.1, 'IND': -0.2, | |
'TEN': -0.6, 'DET': -0.7, 'LAC': -0.7, 'WSH': -0.8, 'MIA': -1.1, 'HOU': -1.1, | |
'ARI': -1.5, 'BUF': -1.9, 'JAX': -4.0, 'CHI': -5.2, 'LAR': -5.3, | |
'NYJ': -6.0, 'CLE': -7.6, 'SF': -7.6 | |
} | |
def get_matchups(): | |
weeks = {} | |
for week_number in [j+1 for j in range(17)]: | |
r = requests.get(base_url+'/nfl/schedule/_/week/'+str(week_number)) | |
soup = bs(r.content, 'html.parser') | |
matchups = [] | |
for link in soup.find_all('a'): | |
href = link.get('href') | |
if '?gameId' in href: | |
m = requests.get(base_url+href) | |
matchup_soup = bs(m.content, 'html.parser') | |
for s in matchup_soup.find_all('span'): | |
class_name = s.get('class') | |
if not class_name: | |
continue | |
if 'home-team' in class_name: | |
away = str(s.string) | |
elif 'away-team' in class_name: | |
home = str(s.string) | |
elif 'value-home' in class_name: | |
home_val = round(float(s.string.strip('%'))/100., 3) | |
elif 'value-away' in class_name: | |
away_val = round(float(s.string.strip('%'))/100., 3) | |
score = round(abs(FPI[home] - FPI[away]) * abs(home_val - away_val), 3) | |
matchups.append(Stats(home, home_val, away, away_val, score)) | |
weeks[week_number] = matchups | |
return weeks | |
if __name__ == '__main__': | |
weeks = get_matchups() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment