-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.py
353 lines (301 loc) · 12.7 KB
/
crawler.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from ast import In
from lxml import html
import requests
import pandas as pd
from bs4 import BeautifulSoup
import re
def get_clubs_data():
html_text = requests.get('https://www.premierleague.com/clubs/').text
soup = BeautifulSoup(html_text, 'lxml')
clubs = soup.find_all('div', class_='indexSection')
clubs = BeautifulSoup(str(clubs), 'lxml')
clubNames = clubs.find_all('h4', class_='clubName')
clubNames = [clubName.text for clubName in clubNames]
Stadiums = clubs.find_all('div', class_='stadiumName')
Stadiums = [Stadium.text for Stadium in Stadiums]
Websites = clubs.find_all('a')
Websites = ["https://www.premierleague.com" + Website["href"]
for Website in Websites]
club_df = pd.DataFrame({'Club':clubNames, 'Stadium':Stadiums, 'Website':Websites})
club_df.to_csv('clubs.csv', index=False)
print("Clubs data saved to clubs.csv")
stadiumLinks = [Website.replace("overview", "stadium") for Website in Websites]
stadiumCapacities = []
stadiumAddresses = []
stadiumPitchSizes = []
stadiumRPLAttendances = []
stadiumBuildDates = []
for i in range(0, 20):
# Get the stadium page
stadiumPage = requests.get(stadiumLinks[i])
stadiumTree = html.fromstring(stadiumPage.content)
# Get the stadium capacity
stadiumCapacity = stadiumTree.cssselect('p:nth-child(1)')[0].text_content()
stadiumCapacity = stadiumCapacity[-6:]
stadiumCapacities.append(stadiumCapacity)
# Get the stadium address, pitch size, RPL attendance and build date
stadiumRPLAttendance = "Unknown"
if i == 14:
stadiumLocation = stadiumTree.cssselect(
'p:nth-child(6)')[0].text_content()
stadiumPitchSize = stadiumTree.cssselect(
'p:nth-child(5)')[1].text_content()
stadiumBuildDate = stadiumTree.cssselect(
'p:nth-child(3)')[1].text_content()
elif i in [3, 15]:
stadiumLocation = stadiumTree.cssselect(
'p:nth-child(4)')[1].text_content()
stadiumPitchSize = stadiumTree.cssselect(
'p:nth-child(3)')[0].text_content()
stadiumBuildDate = stadiumTree.cssselect(
'p:nth-child(2)')[0].text_content()
elif i in [2, 8, 9, 17]:
stadiumLocation = stadiumTree.cssselect(
'p:nth-child(4)')[1].text_content()
stadiumPitchSize = stadiumTree.cssselect(
'p:nth-child(3)')[1].text_content()
stadiumBuildDate = stadiumTree.cssselect(
'p:nth-child(2)')[0].text_content()
else:
stadiumLocation = stadiumTree.cssselect(
'p:nth-child(5)')[1].text_content()
stadiumPitchSize = stadiumTree.cssselect(
'p:nth-child(4)')[1].text_content()
stadiumRPLAttendance = stadiumTree.cssselect(
'p:nth-child(2)')[0].text_content()
stadiumBuildDate = stadiumTree.cssselect(
'p:nth-child(3)')[1].text_content()
stadiumAddresses.append(stadiumLocation[17:])
stadiumPitchSizes.append(stadiumPitchSize[12:])
if (stadiumRPLAttendance != "Unknown"):
stadiumRPLAttendances.append(stadiumRPLAttendance[22:])
else:
stadiumRPLAttendances.append(stadiumRPLAttendance)
stadiumBuildDates.append(stadiumBuildDate[-4:])
# Create a dataframe to store the data
stadium_df = pd.DataFrame({'Stadium': Stadiums,
'Capacity': stadiumCapacities,
'Address': stadiumAddresses,
'Pitch Size': stadiumPitchSizes,
'RPL Attendance': stadiumRPLAttendances,
'Build Date': stadiumBuildDates})
# Save the dataframe to a csv file
stadium_df.to_csv('stadiums.csv', index=False, encoding='utf-8')
print("Stadiums data saved to stadiums.csv")
def get_players_data():
html_text = requests.get('https://www.premierleague.com/clubs/').text
soup = BeautifulSoup(html_text, 'lxml')
clubs = soup.find_all('div', class_='indexSection')
clubs = BeautifulSoup(str(clubs), 'lxml')
Websites = clubs.find_all('a')
Websites = ["https://www.premierleague.com" + Website["href"]
for Website in Websites]
teamLinks = [Website.replace("overview", "squad") for Website in Websites]
#Create empty lists for player
playerLink1 = []
playerCountries = []
playerPositions = []
#For each team link page...
for i in range(len(teamLinks)):
#...Download the team page and process the html code...
squadPage = requests.get(teamLinks[i])
squadTree = html.fromstring(squadPage.content)
#...Extract the player links...
playerLocation = squadTree.cssselect('.playerOverviewCard')
playerCountry = squadTree.cssselect('.playerCountry')
playerPosition = squadTree.cssselect('.position')
#...For each player link within the team page...
for i in range(len(playerLocation)):
#...Save the link, complete with domain...
playerLink1.append("http://www.premierleague.com/" +
playerLocation[i].attrib['href'])
playerCountries.append(playerCountry[i].text_content())
playerPositions.append(playerPosition[i].text_content())
# Create lists for each variable
Name = []
Team = []
Age = []
HeightCM = []
WeightKG = []
df = pd.DataFrame(
{
'Player':[],
'Season':[],
'Team':[]
}
)
df.to_csv('player_career.csv', index=False)
#Populate lists with each player
#For each player...
for i in range(len(playerLink1)):
#...download and process the two pages collected earlier...
playerPage1 = requests.get(playerLink1[i])
playerTree1 = html.fromstring(playerPage1.content)
#...find the relevant datapoint for each player, starting with name...
tempName = str(playerTree1.cssselect('div.name')[0].text_content())
tempSeasons = playerTree1.cssselect('.season p')
tempTeams = playerTree1.cssselect('span.long')
Season = []
prevTeam = []
if len(tempSeasons) > 5:
s = 5
else:
s = len(tempSeasons)
for i in range(s):
Season.append(str(tempSeasons[i].text_content()))
for i in range(s):
prevTeam.append(str(tempTeams[i].text_content()))
df = pd.DataFrame({
'Player': tempName,
'Season': Season,
'Team': prevTeam
})
df.to_csv('player_career.csv', index=False, mode='a', header=False)
#...and team, but if there isn't a team, return "BLANK"...
try:
tempTeam = str(playerTree1.cssselect(
'.table:nth-child(1) .long')[0].text_content())
except IndexError:
tempTeam = str("NULL")
#...and age, but if this isn't there, leave a blank 'no number' number...
try:
tempAge = str(playerTree1.cssselect(
'.pdcol2 li:nth-child(1) .info')[0].text_content())
except IndexError:
tempAge = '0'
#...and height. Needs tidying again...
try:
tempHeight = playerTree1.cssselect(
'.pdcol3 li:nth-child(1) .info')[0].text_content()
tempHeight = int(re.search(r'\d+', tempHeight).group())
except IndexError:
tempHeight = '0'
#...and weight. Same with tidying and returning blanks if it isn't there
try:
tempWeight = playerTree1.cssselect(
'.pdcol3 li+ li .info')[0].text_content()
tempWeight = int(re.search(r'\d+', tempWeight).group())
except IndexError:
tempWeight = '0'
#Now that we have a player's full details - add them all to the lists
Name.append(tempName)
Team.append(tempTeam)
Age.append(tempAge.strip())
HeightCM.append(tempHeight)
WeightKG.append(tempWeight)
#Create data frame from lists
player_df = pd.DataFrame(
{'Team': Team,
'Name': Name,
'Position': playerPositions,
'Nationality': playerCountries,
'Age': Age,
'HeightCM': HeightCM,
'WeightKG': WeightKG})
player_df.to_csv('players.csv', index=False)
print("Players data saved to players.csv")
def get_matches_data():
# Here I will use a different website to scrape the data for the matches
fbref = 'https://fbref.com/en/comps/9/schedule/Premier-League-Scores-and-Fixtures'
page = requests.get(fbref)
tree = html.fromstring(page.content)
matchReports = tree.cssselect('.left~ .left+ .left a')
matchDate = []
matchSeason = '2022/2023'
matchResult = []
matchStadium = []
hTeam = []
aTeam = []
hPossession = []
aPossession = []
hGoals = []
aGoals = []
hFouls = []
aFouls = []
hShots = []
aShots = []
hYCards = []
aYCards = []
hRCards = []
aRCards = []
for i in range(len(matchReports)):
link = matchReports[i].attrib['href']
if 'stathead' in link:
continue
reportPage = requests.get('https://fbref.com' + link)
reportTree = html.fromstring(reportPage.content)
tempTeams = reportTree.cssselect('.logo+ strong a')
tempHTeam = tempTeams[0].text_content()
tempATeam = tempTeams[1].text_content()
tempStadium = reportTree.cssselect('div:nth-child(6) strong+ small')[0].text_content()
tempDate = reportTree.cssselect('.scorebox_meta strong a')[0].text_content()
tempResult = reportTree.cssselect('.score')
tempHGoals = int(tempResult[0].text_content())
tempAGoals = int(tempResult[1].text_content())
tempResult = str(tempHGoals) + ' : ' + str(tempAGoals)
tempPossession = reportTree.cssselect('tr:nth-child(3) strong')
tempHPossession = tempPossession[0].text_content()
tempAPossession = tempPossession[1].text_content()
tempShots = reportTree.cssselect('tr:nth-child(7) td > div > div:nth-child(1)')
tempHShots = tempShots[0].text_content()
tempAShots = tempShots[1].text_content()
if tempHShots[-2] == '0':
if tempHShots[-3] == '0':
tempHShots = int(tempHShots[-9:-7].strip())
else:
tempHShots = int(tempHShots[-7:-5].strip())
else:
tempHShots = int(tempHShots[-8:-6].strip())
tempAShots = int(tempAShots[-2:].strip())
tempFouls = reportTree.cssselect('#team_stats_extra div:nth-child(1) div:nth-child(6) , #team_stats_extra div:nth-child(1) div:nth-child(4)')
tempHFouls = int(tempFouls[0].text_content())
tempAFouls = int(tempFouls[1].text_content())
tempYCards = reportTree.cssselect('tfoot .right:nth-child(13)')
tempHYCards = tempYCards[0].text_content()
tempAYCards = tempYCards[6].text_content()
tempRCards = reportTree.cssselect('tfoot .right:nth-child(14)')
tempHRCards = tempRCards[0].text_content()
tempARCards = tempRCards[6].text_content()
matchDate.append(tempDate)
matchStadium.append(tempStadium)
matchResult.append(tempResult)
hTeam.append(tempHTeam)
aTeam.append(tempATeam)
hPossession.append(tempHPossession)
aPossession.append(tempAPossession)
hGoals.append(tempHGoals)
aGoals.append(tempAGoals)
hShots.append(tempHShots)
aShots.append(tempAShots)
hFouls.append(tempHFouls)
aFouls.append(tempAFouls)
hYCards.append(tempHYCards)
aYCards.append(tempAYCards)
hRCards.append(tempHRCards)
aRCards.append(tempARCards)
match_df = pd.DataFrame({
"Date": matchDate,
"Season": matchSeason,
"Home Team": hTeam,
"Away Team": aTeam,
"Stadium": matchStadium,
"Result": matchResult,
"Home Goals": hGoals,
"Away Goals": aGoals,
"Home Possession": hPossession,
"Away Possession": aPossession,
"Home Shots": hShots,
"Away Shots": aShots,
"Home Fouls": hFouls,
"Away Fouls": aFouls,
"Home Yellow Cards": hYCards,
"Away Yellow Cards": aYCards,
"Home Red Cards": hRCards,
"Away Red Cards": aRCards
})
match_df.to_csv('matches.csv', index=False)
print("Matches data saved to matches.csv")
if __name__ == "__main__":
get_clubs_data()
get_players_data()
get_matches_data()