def _add_defense(player, projections): """Add defense to projection data.""" projection = {} valid = True for cell in player.contents: if cell.div: team = cell.div['class'][1].split('-')[1] projection['name'] = get_team(team) projection['position'] = 'dst' projection['team'] = get_team(team) elif 'stat' in cell['class'][0]: if cell['class'][1] in STAT_MAP: statistic = STAT_MAP[cell['class'][1]] projection[statistic] = float(cell.string) if cell.string.strip() != '-' else 0.0 if valid: calculate_ppr(projection) projections[projection['name']] = projection
def _add_player(player, projections): """Add player to projection data.""" projection = {} valid = True for cell in player.contents: # Get player, position, and team if 'playerNameAndInfo' in cell['class'][0]: name = cell.find(class_='playerName').string # Searching for 'em' seems broken. Making do with this loop for now. team = None position = None for item in cell.div.contents: if item.name == 'em': if '-' in item.string: position, team = item.string.split(' - ') break else: position = item.string.strip() if team is None: valid = False break projection['team'] = get_team(team) projection['position'] = get_position(position) projection['name'] = get_name(name, team, position) elif 'playerOpponent' in cell['class'][0]: # Get opponent if cell.string[0] == '@': # away game projection['home'] = False projection['opponent'] = cell.string[1:] else: projection['home'] = True projection['opponent'] = cell.string elif 'stat' in cell['class'][0]: if cell['class'][1] in STAT_MAP: statistic = STAT_MAP[cell['class'][1]] projection[statistic] = float(cell.string) if cell.string.strip() != '-' else 0.0 if valid: calculate_ppr(projection) projections[projection['name']] = projection
def crawl_espn_projection_page(projections=None, next_page=None, **params): """Crawls the ESPN Page and returns the projection data as a dict""" if next_page: response = requests.get(next_page) else: response = requests.get(ESPN_PROJECTIONS_URL, params) print response.url soup = BeautifulSoup(response.content, 'html.parser') pagination_nav = soup.body.find(class_='paginationNav') for item in pagination_nav.find_all('a'): if 'NEXT' in item.contents: next_page = item['href'] projections = {} if projections is None else projections player_rows = soup.body(class_='pncPlayerRow') for row in player_rows: projection = {} valid = True for i, cell in enumerate(row.find_all('td')): if i == 0: # Find Name, Team, and Position name = cell.a.string if 'D/ST' in cell.contents[1]: team = get_team(cell.contents[0].string.split()[0].strip().lower()) projection['name'] = get_name(name, team, 'dst') projection['team'] = get_team(team) projection['position'] = 'dst' else: splits = cell.contents[1].split() team = splits[1] position = splits[2] # No Free Agents if team == 'FA': valid = False break projection['name'] = get_name(name, team, position) projection['team'] = get_team(team) try: projection['position'] = get_position(position) except Exception: # Remove kickers and the like. valid = False break if i == 1: # Find opponent and whether or not team is home or away if cell.a is None: valid = False break text = cell.a.string if text[0] == '@': projection['home'] = False projection['opponent'] = get_team(text[1:]) else: projection['home'] = True projection['opponent'] = get_team(text) elif i == 3: projection['receptions'] = float(cell.string.split('/', 1)[0]) elif i in range(4, 14): _populate_stats(i, cell, projection) if valid: calculate_ppr(projection) projections[projection['name']] = projection if next_page and len(projections) < 500: time.sleep(0.250) return crawl_espn_projection_page(projections=projections, next_page=next_page) else: return projections