-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_crawler.py
336 lines (294 loc) · 13.5 KB
/
fs_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
from bs4 import BeautifulSoup
import requests
import fs_utils
from model.Match import Match
from model.Match import Score
from model.Match import MatchDetail
from model.Match import MatchStats
from model.Match import MatchSquad
from model.LeagueTable import LeagueTableItem
from model.LeagueTable import LeagueTable
def get_content(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
[content] = soup.select('body > div.wrapper > div.content')
extract_tag(content, 'div', 'cal-wrap')
extract_tag(content, 'div', 'row mt4 bb bt')
extract_tag(content, 'div', 'row row-tall mt4')
extract_tag(content, 'div', 'cal clear')
return content
def get_details(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
details = soup.find_all('div', attrs={'data-id': 'details'})
newDetails = []
for detail in details:
if detail.find('div', class_='min') is not None:
newDetails.append(detail)
return newDetails
def get_stats(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
stats = soup.find('div', attrs={'data-id': 'stats'})
extract_tag(stats, 'div', 'row mt4 bt')
return stats
def get_squad(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
return soup.find('div', attrs={'data-id': 'substitutions'})
def extract_tag(tree, tag, css_class):
for t in tree.find_all(tag, class_=css_class):
t.extract()
def get_match_squad(match):
squadTag = get_squad(match.detailUrl)
lineupTag = []
substitutionsTag = []
substitutesTag = []
itemTag = squadTag.find('div', class_='row mt4 bt')
formationTag = itemTag.find_next_sibling()
itemTag = formationTag.find_next_sibling()
nextTag = itemTag.find_next_sibling()
while True:
if nextTag.string is not None and nextTag.string.strip() == 'substitutions :':
break
else:
lineupTag.append(nextTag)
nextTag = nextTag.find_next_sibling()
nextTag = nextTag.find_next_sibling()
while True:
if nextTag.string is not None and nextTag.string.strip() == 'substitute players :':
break
else:
substitutionsTag.append(nextTag)
nextTag = nextTag.find_next_sibling()
nextTag = nextTag.find_next_sibling()
while True:
if nextTag.string is not None and nextTag.string.strip() == 'coach :':
break
else:
substitutesTag.append(nextTag)
nextTag = nextTag.find_next_sibling()
coachTag = nextTag.find_next_sibling()
homeFormation = None
awayFormation = None
if formationTag is not None:
for (idx, iTag) in enumerate(formationTag.find_all('i')):
if idx == 0:
homeFormation = iTag.find_parent().contents[1].strip()
elif idx == 1:
awayFormation = iTag.find_parent().contents[1].strip()
else:
pass
else:
homeFormation = None
awayFormation = None
if lineupTag is not None and lineupTag != []:
homeLineup = []
awayLineup = []
for tag in lineupTag:
for (idx, iTag) in enumerate(tag.find_all('i')):
if idx == 0:
homeLineup.append(iTag.find_parent().contents[2].strip())
elif idx == 1:
awayLineup.append(iTag.find_parent().contents[2].strip())
else:
pass
else:
homeLineup = None
awayLineup = None
if substitutionsTag is not None and substitutionsTag != []:
homeSubstitutions = []
awaySubstitutions = []
for tag in substitutionsTag:
for (idx, timeTag) in enumerate(tag.find_all('div', class_='col-1 ln36')):
subTag = timeTag.find_next_sibling()
if idx == 0:
subTime = timeTag.string.strip() if timeTag.string is not None else None
if subTime is not None:
subOut = subTag.find('i', class_='inc sub-out').find_parent().contents[1].strip()
subIn = subTag.find('i', class_='inc sub-in').find_parent().contents[1].strip()
homeSubstitution = {'subTime': subTime, 'subOut': subOut, 'subIn': subIn}
homeSubstitutions.append(homeSubstitution)
elif idx == 1:
subTime = timeTag.string.strip() if timeTag.string is not None else None
if subTime is not None:
subOut = subTag.find('i', class_='inc sub-out').find_parent().contents[1].strip()
subIn = subTag.find('i', class_='inc sub-in').find_parent().contents[1].strip()
awaySubstitution = {'subTime': subTime, 'subOut': subOut, 'subIn': subIn}
awaySubstitutions.append(awaySubstitution)
else:
pass
else:
homeSubstitutions = None
awaySubstitutions = None
if substitutesTag is not None and substitutesTag != []:
homeSubstitutes = []
awaySubstitutes = []
for tag in substitutesTag:
for (idx, iTag) in enumerate(tag.find_all('i')):
if idx == 0:
homeSubstitutes.append(iTag.find_parent().contents[2].strip())
elif idx == 1:
awaySubstitutes.append(iTag.find_parent().contents[2].strip())
else:
pass
else:
homeSubstitutes = None
awaySubstitutes = None
homeCoach = None
awayCoach = None
if coachTag is not None:
for (idx, iTag) in enumerate(coachTag.find_all('i')):
if idx == 0:
homeCoach = iTag.find_parent().contents[1].strip()
elif idx == 1:
awayCoach = iTag.find_parent().contents[1].strip()
else:
pass
else:
homeCoach = None
awayCoach = None
return MatchSquad(match.home, match.away, match.score,
homeFormation, homeLineup, homeSubstitutions, homeSubstitutes, homeCoach,
awayFormation, awayLineup, awaySubstitutions, awaySubstitutes, awayCoach)
def get_match_stats(match):
stats = get_stats(match.detailUrl)
statNameTags = stats.find_all('div', class_='col-2 tcenter')
matchStatList = []
for statNameTag in statNameTags:
statNumHomeTag = statNameTag.find_previous_sibling().find_previous_sibling()
statNumAwayTag = statNameTag.find_next_sibling().find_next_sibling()
statNumHome = statNumHomeTag.string.strip() if statNumHomeTag.string is not None else None
statNumAway = statNumAwayTag.string.strip() if statNumAwayTag.string is not None else None
if statNumHome is not None and statNumAway is not None:
matchStat = {'statName': statNameTag.string.strip(),
'homeStatNum': int(statNumHome), 'awayStatNum': int(statNumAway)}
matchStatList.append(matchStat)
return MatchStats(match.home, match.away, match.score, matchStatList)
def get_match_detail(match):
details = get_details(match.detailUrl)
events = []
for detail in details:
timeTag = detail.find('div', class_='min')
homeTag = timeTag.find_next_sibling()
scoreTag = homeTag.find_next_sibling()
awayTag = scoreTag.find_next_sibling()
if timeTag is None or homeTag is None or scoreTag is None or awayTag is None:
raise Exception("detail: {} unexpected".format(detail))
eventTime = timeTag.string.strip() if timeTag.string is not None else None
eventScore = scoreTag.string.strip() if scoreTag.string is not None else None
eventScore = eventScore if events != '' else None
homeEventPlayer = homeTag.find('span', class_='name').string
homeEventPlayer = homeEventPlayer.strip() if homeEventPlayer is not None else None
homeEventAssist = None
if homeTag.find('span', class_='inc goal') is not None:
goalHow = homeTag.find('span', class_='ml4').string
if goalHow is None:
homeEventType = None
elif goalHow.strip() == '(pen.)':
homeEventType = "penalty"
elif goalHow.strip() == '(o.g.)':
homeEventType = "owngoal"
else:
homeEventType = "goal"
homeEventAssistTag = homeTag.find('span', class_='assist name')
if homeEventAssistTag is not None:
homeEventAssist = homeEventAssistTag.string.replace('(assist)', '')
homeEventAssist = homeEventAssist.strip()
elif homeTag.find('span', class_='inc yellowcard') is not None:
homeEventType = "yellowcard"
elif homeTag.find('span', class_='inc redyellowcard') is not None:
homeEventType = "redyellowcard"
elif homeTag.find('span', class_='inc redcard') is not None:
homeEventType = "redcard"
else:
homeEventType = None
eventHome = {"type": homeEventType, "player": homeEventPlayer, "assist": homeEventAssist}
awayEventPlayer = awayTag.find('span', class_='name').string
awayEventPlayer = awayEventPlayer.strip() if awayEventPlayer is not None else None
awayEventAssist = None
if awayTag.find('span', class_='inc goal') is not None:
goalHow = awayTag.find('span', class_='mr4').string
if goalHow is None:
awayEventType = None
elif goalHow.strip() == '(pen.)':
awayEventType = "penalty"
elif goalHow.strip() == '(o.g.)':
awayEventType = "owngoal"
else:
awayEventType = "goal"
awayEventAssistTag = awayTag.find('span', class_='assist name')
if awayEventAssistTag is not None:
awayEventAssist = awayEventAssistTag.string.replace('(assist)', '')
awayEventAssist = awayEventAssist.strip()
elif awayTag.find('span', class_='inc yellowcard') is not None:
awayEventType = "yellowcard"
elif awayTag.find('span', class_='inc redyellowcard') is not None:
awayEventType = "redyellowcard"
elif awayTag.find('span', class_='inc redcard') is not None:
awayEventType = "redcard"
else:
awayEventType = None
eventAway = {"type": awayEventType, "player": awayEventPlayer, "assist": awayEventAssist}
event = {'time': eventTime, 'home': eventHome, 'score': eventScore, 'away': eventAway}
events.append(event)
return MatchDetail(match.home, match.away, match.score, events)
def get_score(country, leagueName):
url = fs_utils.get_url(country, leagueName)
content = get_content(url)
matchList = []
for minTag in content.select('div.min'):
rowTag = minTag.find_parent()
minStr = minTag.string.strip() if minTag.string is not None else minTag.contents[1].strip()
if minStr == 'FT':
matchStatus = 'played'
elif ':' in minStr:
matchStatus = 'not_play'
else:
matchStatus = 'playing'
matchTime = "{} {}".format(fs_utils.get_date(rowTag['data-esd']), minStr)
homeTeamTag = minTag.find_next_sibling()
scoreTag = homeTeamTag.find_next_sibling()
awayTeamTag = scoreTag.find_next_sibling()
detailUrl = "http://www.livescores.com" + scoreTag.a['href']
match = Match(matchTime, homeTeamTag.string, scoreTag.a.string, awayTeamTag.string,
matchStatus, detailUrl)
matchList.append(match)
return Score(country, leagueName, matchList)
def get_league_table(country, leagueName):
url = fs_utils.get_url(country, leagueName)
content = get_content(url)
leagueTableItems = []
for (idx, teamTag) in enumerate(content.select('div.team')):
if idx > 0:
rankTag = teamTag.find_previous_sibling().find('span', class_=None)
playedTag = teamTag.find_next_sibling()
winsTag = playedTag.find_next_sibling()
drawsTag = winsTag.find_next_sibling()
lostsTag = drawsTag.find_next_sibling()
goalsForTag = lostsTag.find_next_sibling()
goalsAgainstTag = goalsForTag.find_next_sibling()
goalsDiffTag = goalsAgainstTag.find_next_sibling()
pointsTag = goalsDiffTag.find_next_sibling()
leagueTableItem = LeagueTableItem(rankTag.string, teamTag.string, playedTag.string, winsTag.string,
drawsTag.string, lostsTag.string, goalsForTag.string,
goalsAgainstTag.string, goalsDiffTag.string, pointsTag.string,
country, leagueName)
leagueTableItems.append(leagueTableItem)
return LeagueTable(country, leagueName, leagueTableItems)
# leagueTable = get_league_table("China", "Super League")
# leagueTable.print()
#
# score = get_score("China", "Super League")
# score.print()
# match = score.matchList[1]
# matchDetail = get_match_detail(match)
# matchDetail.print()
# matchStats = get_match_stats(match)
# matchStats.print()
# if match.status == 'not_play' or match.detailUrl is None or match.detailUrl == '':
# print("match info not available yet")
# else:
# matchSquad = get_match_squad(match)
# matchSquad.print()
# fc_utils.is_reachable('www.livescores.com')