-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnba_result_scrape.py
72 lines (53 loc) · 2.54 KB
/
nba_result_scrape.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from bs4 import BeautifulSoup
import requests
from datetime import date, timedelta
import sys
import os
import django
sys.path.append('spread')
os.environ['DJANGO_SETTINGS_MODULE'] = 'spread.settings'
django.setup()
from nba.models import Game, Pick, Nba_Record
today = date.today()
yesterday = today - timedelta(days=1)
# deployment '/home/kilgoretrout1/spread/csv_files/results.csv'
teams = {'Atlanta': 'Atlanta Hawks', 'Boston': 'Boston Celtics', 'Brooklyn': 'Brooklyn Nets',
'Charlotte': 'Charlotte Hornets', 'Chicago': 'Chicago Bulls', 'Cleveland': 'Cleveland Cavaliers',
'Dallas': 'Dallas Mavericks', 'Denver': 'Denver Nuggets', 'Detroit': 'Detroit Pistons',
'Golden State': 'Golden State Warriors', 'Houston': 'Houston Rockets', 'Indiana': 'Indiana Pacers', 'LA Clippers': 'Los Angeles Clippers',
'LA Lakers': 'Los Angeles Lakers', 'Memphis': 'Memphis Grizzlies', 'Miami': 'Miami Heat', 'Milwaukee': 'Milwaukee Bucks',
'Minnesota': 'Minnesota Timberwolves', 'New Orleans':'New Orleans Pelicans', 'New York': 'New York Knicks',
'Oklahoma City': 'Oklahoma City Thunder', 'Orlando': 'Orlando Magic', 'Philadelphia': 'Philadelphia 76ers', 'Phoenix': 'Phoenix Suns',
'Portland': 'Portland Trail Blazers', 'Sacramento': 'Sacramento Kings', 'San Antonio': 'San Antonio Spurs',
'Toronto': 'Toronto Raptors', 'Utah': 'Utah Jazz', 'Washington': 'Washington Wizards'}
source = f"https://www.basketball-reference.com/boxscores/?month={yesterday.month}&day={yesterday.day}&year={yesterday.year}"\
page = requests.get(source)
soup = BeautifulSoup(page.text, 'lxml')
tables = soup.find("div", class_="game_summaries")
games = tables.find_all("div", class_="game_summary expanded nohover")
scores = []
for game in games:
score = []
for td in game.find_all("td"):
if td.text != "":
score.append(td.text.strip())
clean_score = score[:5]
clean_score[0] = teams[clean_score[0]]
clean_score[3] = teams[clean_score[3]]
clean_score.pop(2)
scores.append(clean_score)
for score in scores:
games = Game.objects.filter(date_time__date=yesterday)
for game in games:
if score[0] == game.away_team and score[2] == game.home_team:
game.away_score = float(score[1])
game.home_score = float(score[3])
game.save()
picks = Pick.objects.filter(game__date_time__date=yesterday)
for pick in picks:
pick.get_outcome()
pick.save()
records = Nba_Record.objects.all()
for rec in records:
rec.update_record()
rec.save()