-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.js
221 lines (195 loc) · 8.72 KB
/
scrape.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const puppeteer = require('puppeteer');
const scrapeTeam = async team => {
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const scrapeSeasonAndPlayers = async (year) => {
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36');
try {
// First get player stats
console.log(`Scraping ${team} ${year} player stats...`);
await page.goto(`https://www.basketball-reference.com/teams/${team}/${year}.html`);
await page.waitForSelector('#per_game_stats', { visible: true, timeout: 10000 });
const playerStats = await page.evaluate(() => {
const rows = Array.from(document.querySelectorAll('#per_game_stats tbody tr'))
.filter(row => !row.classList.contains('thead'));
return rows.map(row => {
const getValue = (stat) => {
let cell;
switch(stat) {
case "player":
cell = row.querySelector('[data-stat="name_display"]');
break;
default:
cell = row.querySelector(`[data-stat="${stat}"]`);
}
return cell ? cell.textContent.trim() : null;
};
const parseNumeric = (value) => {
if (!value) return null;
const parsed = parseFloat(value);
return isNaN(parsed) ? null : parsed;
};
return {
name: getValue("player"),
position: getValue("pos"),
age: parseNumeric(getValue("age")),
gamesPlayed: parseNumeric(getValue("games")),
gamesStarted: parseNumeric(getValue("games_started")),
minutesPerGame: parseNumeric(getValue("mp_per_g")),
fieldGoalsPerGame: parseNumeric(getValue("fg_per_g")),
fieldGoalAttempts: parseNumeric(getValue("fga_per_g")),
fieldGoalPct: parseNumeric(getValue("fg_pct")),
threePtPerGame: parseNumeric(getValue("fg3_per_g")),
threePtAttempts: parseNumeric(getValue("fg3a_per_g")),
threePtPct: parseNumeric(getValue("fg3_pct")),
freeThrowsPerGame: parseNumeric(getValue("ft_per_g")),
freeThrowAttempts: parseNumeric(getValue("fta_per_g")),
freeThrowPct: parseNumeric(getValue("ft_pct")),
reboundsPerGame: parseNumeric(getValue("trb_per_g")),
assistsPerGame: parseNumeric(getValue("ast_per_g")),
stealsPerGame: parseNumeric(getValue("stl_per_g")),
blocksPerGame: parseNumeric(getValue("blk_per_g")),
pointsPerGame: parseNumeric(getValue("pts_per_g"))
};
}).filter(player => player && player.name);
});
// Then get game data
console.log(`Scraping ${team} ${year} game stats...`);
await page.goto(`https://www.basketball-reference.com/teams/${team}/${year}/gamelog/`);
await page.waitForSelector('#tgl_basic', { visible: true, timeout: 10000 });
const gameStats = await page.evaluate(() => {
let currentWins = 0;
let currentLosses = 0;
let currentStreak = { type: null, count: 0 };
const rows = Array.from(document.querySelectorAll('#tgl_basic tbody tr'))
.filter(row => !row.classList.contains('thead') && row.querySelector('[data-stat="date_game"]'));
return rows.map(row => {
const getValue = (stat) => {
const cell = row.querySelector(`[data-stat="${stat}"]`);
return cell ? cell.textContent.trim() : null;
};
const parseNumeric = (value) => {
if (!value) return null;
const parsed = parseFloat(value);
return isNaN(parsed) ? null : parsed;
};
// Update wins/losses based on game result
const result = getValue("game_result");
if (result === 'W') {
currentWins++;
if (currentStreak.type === 'W') {
currentStreak.count++;
} else {
currentStreak = { type: 'W', count: 1 };
}
} else if (result === 'L') {
currentLosses++;
if (currentStreak.type === 'L') {
currentStreak.count++;
} else {
currentStreak = { type: 'L', count: 1 };
}
}
// Calculate defensive rebounds
const totalReb = parseNumeric(getValue("trb")) || 0;
const offReb = parseNumeric(getValue("orb")) || 0;
const defReb = totalReb - offReb;
const oppTotalReb = parseNumeric(getValue("opp_trb")) || 0;
const oppOffReb = parseNumeric(getValue("opp_orb")) || 0;
const oppDefReb = oppTotalReb - oppOffReb;
return {
date: getValue("date_game"),
homeGame: getValue("game_location") !== '@',
opponent: getValue("opp_id"),
result: result,
teamPoints: parseNumeric(getValue("pts")),
oppPoints: parseNumeric(getValue("opp_pts")),
wins: currentWins,
losses: currentLosses,
streak: `${currentStreak.type} ${currentStreak.count}`,
// Team stats
teamFG: parseNumeric(getValue("fg")),
teamFGA: parseNumeric(getValue("fga")),
teamFGPct: parseNumeric(getValue("fg_pct")),
team3P: parseNumeric(getValue("fg3")),
team3PA: parseNumeric(getValue("fg3a")),
team3PPct: parseNumeric(getValue("fg3_pct")),
teamFT: parseNumeric(getValue("ft")),
teamFTA: parseNumeric(getValue("fta")),
teamFTPct: parseNumeric(getValue("ft_pct")),
teamORB: parseNumeric(getValue("orb")),
teamDRB: defReb || null,
teamTRB: parseNumeric(getValue("trb")),
teamAST: parseNumeric(getValue("ast")),
teamSTL: parseNumeric(getValue("stl")),
teamBLK: parseNumeric(getValue("blk")),
teamTOV: parseNumeric(getValue("tov")),
teamPF: parseNumeric(getValue("pf")),
// Opponent stats
oppFG: parseNumeric(getValue("opp_fg")),
oppFGA: parseNumeric(getValue("opp_fga")),
oppFGPct: parseNumeric(getValue("opp_fg_pct")),
opp3P: parseNumeric(getValue("opp_fg3")),
opp3PA: parseNumeric(getValue("opp_fg3a")),
opp3PPct: parseNumeric(getValue("opp_fg3_pct")),
oppFT: parseNumeric(getValue("opp_ft")),
oppFTA: parseNumeric(getValue("opp_fta")),
oppFTPct: parseNumeric(getValue("opp_ft_pct")),
oppORB: parseNumeric(getValue("opp_orb")),
oppDRB: oppDefReb || null,
oppTRB: parseNumeric(getValue("opp_trb")),
oppAST: parseNumeric(getValue("opp_ast")),
oppSTL: parseNumeric(getValue("opp_stl")),
oppBLK: parseNumeric(getValue("opp_blk")),
oppTOV: parseNumeric(getValue("opp_tov")),
oppPF: parseNumeric(getValue("opp_pf"))
};
});
});
// Debug: Log game stats
console.log(`Found ${gameStats.length} games for ${year}`);
if (gameStats.length > 0) {
console.log('Sample game data:', gameStats[0]);
}
return {
year,
players: playerStats,
games: gameStats
};
} catch (error) {
console.error(`Error scraping ${year}:`, error);
return { year, players: [], games: [] };
} finally {
await page.close();
}
};
try {
const seasons = {};
// Scrape last 20 seasons
for (let year = 2024; year > 2004; year--) {
console.log(`Starting ${year} season...`);
const seasonData = await scrapeSeasonAndPlayers(year);
seasons[year] = seasonData;
console.log(`Completed ${year} season: ${seasonData.players.length} players, ${seasonData.games.length} games`);
}
await fs.promises.mkdir('./basketball-data', { recursive: true });
await fs.promises.writeFile(
`./basketball-data/${team}.json`,
JSON.stringify(seasons, null, 2)
);
console.log('Data saved successfully');
} catch (e) {
console.error('Scraping error:', e);
} finally {
await browser.close();
}
};
// Run the scraper
(async () => {
await scrapeTeam('GSW');
})();
module.exports = scrapeTeam;