-
Notifications
You must be signed in to change notification settings - Fork 2
/
match_data_extraction_version_1.0.py
353 lines (297 loc) · 9.32 KB
/
match_data_extraction_version_1.0.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
#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: GRSP
#
# Created: 30-05-2013
# Copyright: (c) GRSP 2013
# Licence: <your licence>
#-------------------------------------------------------------------------------
from bs4 import BeautifulSoup
import requests
import csv
import pandas as p
import sys
import os
df = p.read_csv("All_Matches.csv")
newpath = r'Match Details'
if not os.path.exists(newpath): os.makedirs(newpath)
for i in df.index:
url= df.ix[i]['MatchLink']
## url='http://www.11v11.com/matches/warmley-v-southampton-03-october-1891-219170/'
html = requests.get(url).text
soup = BeautifulSoup(html)
fileEntry=[]
match=''
date=''
fileEntry.append(["League", df.ix[i]['League']])
#match name and date
head = soup.html.head.title
try:
match,date=head.text.split("-")[0].strip().split(",")
fileEntry.append(["Match",match.strip()])
fileEntry.append(["Date",date.strip()])
except:
print 'No Match Details.'
fileEntry.append(["Match",""])
fileEntry.append(["Date",""])
print "Count: ",i+1,' Match: ',match+date
#match details
try:
matchDet=soup.find("div", {"class":"basicData"})
table = matchDet.find('table')
rows = table.findAll('tr')
for tr in rows:
cols = tr.findAll('td')
op=[]
for td in cols:
text = ''.join(td.find(text=True))
op.append(text)
fileEntry.append(op)
except:
print 'No Match Basic Details present.'
# home team
homegoals='0'
home=match.split('v')[0].strip()
try:
homeTeam=soup.find("div", {"class":"home"})
try:
home= homeTeam.find("a").text.strip()
except:
home= match.split('v')[0]
fileEntry.append(["Home Team",home])
homegoals= homeTeam.find("span").text.strip()
fileEntry.append(["Home Team Goals",homegoals])
except:
print 'No goals HOME TEAM.'
fileEntry.append(["Home Team",home])
fileEntry.append(["Home Team Goals",homegoals])
# away team
awaygoals='0'
away=match.split('v')[1].strip()
try:
awayTeam=soup.find("div", {"class":"away"})
try:
away=awayTeam.find("a").text.strip()
except:
away=match.split('v')[1].strip()
fileEntry.append(["Away Team",away])
awaygoals=awayTeam.find("span").text.strip()
fileEntry.append(["Away Team Goals",awaygoals])
except:
print 'No Goals AWAY TEAM.'
fileEntry.append(["Away Team",away])
fileEntry.append(["Away Team Goals",'0'])
if((int)(homegoals)==(int)(awaygoals)):
fileEntry.append(["Result","Draw"])
elif((int)(homegoals)>(int)(awaygoals)):
fileEntry.append(["Result",home])
elif((int)(homegoals)<(int)(awaygoals)):
fileEntry.append(["Result",away])
# players
home=soup.find("div", {"class":"lineup"})
##
# home team players
try:
homeTeamPlayers=home.find("div", {"class":"home"})
homeTeamPlayers= homeTeamPlayers.findAll("div")
pl=[]
pl.append("Home Team Players")
for player in homeTeamPlayers:
name=player.find("a").text
position=player.find("span").text
country=''
try:
country= (str)(player).split("/")[4].split(".")[0]
except:
print 'no mention of player country'
pl.append(name+'-'+position+'-'+country)
fileEntry.append(pl)
except:
print 'Home team players not mentioned.'
fileEntry.append(['Home Team Players'])
##
# away team players
try:
awayTeamPlayers=home.find("div", {"class":"away"})
awayTeamPlayers= awayTeamPlayers.findAll("div")
pl=[]
pl.append("Away Team Players")
for player in awayTeamPlayers:
name=player.find("a").text
position=player.find("span").text
country=''
try:
country= (str)(player).split("/")[4].split(".")[0]
except:
print 'no mention of player country'
pl.append(name+'-'+position+'-'+country)
fileEntry.append(pl)
except:
print 'Away Team players not mentioned.'
fileEntry.append(['Away Team Players'])
# substitutions
substitutions=soup.find("div", {"class":"substitutions"})
##
#home
try:
homeSubst=substitutions.find("div", {"class":"home"})
table = homeSubst.find('table')
rows = table.findAll('tr')
op=[]
op.append("Home Team Substitutions")
for tr in rows:
cols = tr.findAll('td')
op2=''
for td in cols:
op2=op2+td.text+"-"
op.append(op2)
fileEntry.append(op)
except:
print 'Home Team No Substitutions.'
fileEntry.append(['Home Team Substitutions'])
#away
try:
awaySubst=substitutions.find("div", {"class":"away"})
table = awaySubst.find('table')
rows = table.findAll('tr')
op=[]
op.append("Away Team Substitutions")
for tr in rows:
cols = tr.findAll('td')
op2=''
for td in cols:
op2=op2+td.text+"-"
op.append(op2)
fileEntry.append(op)
except:
print 'Away Team No Substitutions.'
fileEntry.append(['Away Team Substitutions'])
# goals
goals=soup.find("div", {"class":"goals"})
op=[]
op.append("Home Team Goal Scorers")
#home
try:
homeGoals=goals.find("div", {"class":"home"})
table = homeGoals.find('table')
rows = table.findAll('tr')
for tr in rows:
cols = tr.findAll('td')
op2=''
for td in cols:
op2=op2+td.text.strip()+"-"
op.append(op2)
except:
print 'No Home Team Goal Scorers.'
fileEntry.append(op)
else:
fileEntry.append(op)
#away
op=[]
op.append("Away Team Goal Scorers")
try:
awayGoals=goals.find("div", {"class":"away"})
table = awayGoals.find('table')
rows = table.findAll('tr')
for tr in rows:
cols = tr.findAll('td')
op2=''
for td in cols:
op2=op2+td.text.strip()+"-"
op.append(op2)
except:
print 'No Away Team Goal Scorers.'
fileEntry.append(op)
else:
fileEntry.append(op)
#
# cards
cards=soup.find("div", {"class":"cards"})
op=[]
op.append("Home Team Cards")
try:
#home
homecards=cards.find("div", {"class":"home"})
table = homecards.find('table')
rows = table.findAll('tr')
for tr in rows:
cols = tr.findAll('td')
op2=''
for td in cols:
op2=op2+td.text.strip()+"-"
op.append(op2)
except:
print 'No cards for home team'
fileEntry.append(op)
## else:
## fileEntry.append(op)
op=[]
op.append("Away Team Cards")
try:
#away
awaycards=cards.find("div", {"class":"away"})
table = awaycards.find('table')
rows = table.findAll('tr')
for tr in rows:
cols = tr.findAll('td')
op2=''
for td in cols:
op2=op2+td.text.strip()+"-"
op.append(op2)
except:
print 'No cards for away team'
fileEntry.append(op)
## else:
## fileEntry.append(op)
# on the bench
# home team players
try:
home=soup.findAll("div", {"class":"lineup"})[1]
homeTeamPlayers=home.find("div", {"class":"home"})
homeTeamPlayers= homeTeamPlayers.findAll("div")
pl=[]
pl.append("Home Team Players on Bench")
for player in homeTeamPlayers:
name=player.find("a").text
position=player.find("span").text
country=''
try:
country= (str)(player).split("/")[4].split(".")[0]
except:
print 'No mention of players country'
pl.append(name+'-'+position+'-'+country)
fileEntry.append(pl)
except:
print 'No home team players on bench.'
fileEntry.append(['Home Team Players on Bench'])
# away team players
try:
home=soup.findAll("div", {"class":"lineup"})[1]
awayTeamPlayers=home.find("div", {"class":"away"})
awayTeamPlayers= awayTeamPlayers.findAll("div")
pl=[]
pl.append("Away Team Players on Bench")
for player in awayTeamPlayers:
name=player.find("a").text
position=player.find("span").text
country=''
try:
country= (str)(player).split("/")[4].split(".")[0]
except:
print 'players country not mentioned.'
pl.append(name+'-'+position+'-'+country)
fileEntry.append(pl)
except:
print 'No away team players on bench.'
fileEntry.append(['Away Team Players on Bench'])
try:
fp = open(newpath+'\\'+match+'_'+date+'.csv',"wb")
cot=csv.writer(fp)
for entry in fileEntry:
cot.writerow(entry)
fp.close()
except:
print "File Writing Error."
## sys.exit()