def get_players(file_name): categories = [] players = [] try: with open(file_name, 'r', encoding="utf-8") as file_contents: csv_reader = csv.reader(file_contents, delimiter=',') row_count = 0 for row in csv_reader: player = Player() if row_count == 0: for col in row: if col: categories.append(col) row_count += 1 else: for idx, col in enumerate(row): if col: try: setattr(player, categories[idx], col) if file_name == "per_poss": print("Index: %d, %s: %s" % (idx, categories[idx], col)) except IndexError: print(IndexError) else: pass players.append(player) except OSError: print(OSError) return players
def get_players(file_name): categories = [] players = [] try: with open(file_name, 'r', encoding="utf-8") as file_contents: csv_reader = csv.reader(file_contents, delimiter=',') row_count = 0 for row in csv_reader: player = Player() if row_count == 0: for col in row: if col: categories.append(col) row_count += 1 else: cat_count = 0 for col in row: if col: try: print(categories.index(col)).__setattr__( categories.index(col), col) except IndexError: print("Error") cat_count += 1 players.append(player) print(player.__dict__.keys()) except OSError: print(OSError) return players
def get_players(file_name): categories = [] players = [] try: with open(file_name, 'r', encoding="utf-8") as file_contents: csv_reader = csv.reader(file_contents, delimiter=',') row_count = 0 for row in csv_reader: player = Player() if row_count == 0: for col in row: if col: categories.append(col) row_count += 1 else: cat_count = 0 for col in row: if col: try: setattr(player, categories[cat_count], col) except IndexError: print(IndexError) cat_count += 1 players.append(player) except OSError: print(OSError) return players
def get_players(file_name): categories = [] players = [] try: with open(file_name, 'r') as file_contents: table_name = file_name + "_stats" page_soup = soup(file_contents, "html.parser") html_table = page_soup.find('table', {'id': table_name}) for x in html_table.thead.find_all("th"): stat_cats = x.get("data-stat") for row in page_soup.find_all('tr', {"class": "full_table"}): player = Player() for stat in row.find_all("td"): setattr(player, stat.get('data-stat'), stat.text) players.append(player) except OSError: print(OSError) return players
def pull(url, stat_cats): file_name = url[54:-5] categories = [] players = [] try: with open(file_name, 'r') as csv_file: csv_reader = csv.reader(csv_file) for line in csv_reader: player = Player() if len(line) > 29: del line[29:] print(line) # setattr(player, line, '') if line: for stats in line: stats = stats[2:-1] setattr(player, stats, stats) players.append(player) print(player.player) # print(line) except OSError: print(OSError)
page_soup = soup(html, "html.parser") tables = page_soup.findAll("tr", {"class": "full_table"}) return tables def get_stat(_table, _stat): try: stat = int(_table.find("td", {"data-stat": _stat}).text) setattr(player, _stat, stat) except ValueError: stat = str(_table.find("td", {"data-stat": _stat}).text) setattr(player, _stat, stat) for table in scraper(szn_url): player = Player() setattr(player, "name", str(table.find("td", { "data-stat": "player" }).text)) for each_stat in stats: get_stat(table, each_stat) setattr(player, "ppg", player.pts / player.g) setattr(player, "rpg", player.trb / player.g) setattr(player, "apg", player.ast / player.g) list_of_players.append(player) # scoring_leaders = sorted(list_of_players, key=lambda x: x.ppg, reverse=True) for leader in scoring_leaders:
three_point_attempts = int(player.find("td", {"data-stat": "fg3a"}).text) two_point_made = int(player.find("td", {"data-stat": "fg2"}).text) two_point_attempts = int(player.find("td", {"data-stat": "fg2a"}).text) free_throws_made = int(player.find("td", {"data-stat": "ft"}).text) free_throws_attempted = int(player.find("td", {"data-stat": "fta"}).text) defensive_rebounds = int(player.find("td", {"data-stat": "drb"}).text) offensive_rebounds = int(player.find("td", {"data-stat": "orb"}).text) assists = int(player.find("td", {"data-stat": "ast"}).text) steals = int(player.find("td", {"data-stat": "stl"}).text) blocks = int(player.find("td", {"data-stat": "blk"}).text) turnovers = int(player.find("td", {"data-stat": "tov"}).text) personal_fouls = int(player.find("td", {"data-stat": "pf"}).text) list_of_players.append( Player(name, position, age, team, games_played, games_started, minutes_played, fg_made, fg_att, three_point_made, three_point_attempts, two_point_made, two_point_attempts, free_throws_made, free_throws_attempted, offensive_rebounds, defensive_rebounds, assists, steals, blocks, turnovers, personal_fouls)) scoring_leaders = sorted(list_of_players, key=lambda x: x.ptspg, reverse=True) assists_leaders = sorted(list_of_players, key=lambda x: x.astpg, reverse=True) rebounding_leaders = sorted(list_of_players, key=lambda x: x.t_rebpg, reverse=True) scoring_20 = [] assisting_20 = [] rebounding_20 = [] i = 0 while i < 20: