-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp+_game_data.py
271 lines (193 loc) · 6.94 KB
/
p+_game_data.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
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 25 13:25:22 2021
@author: RB505
"""
from selenium import webdriver
from bs4 import BeautifulSoup
import requests
import openpyxl
from openpyxl import Workbook
from IPython.display import display_html
from requests_html import HTMLSession
import nest_asyncio
import json
import asyncio
import openpyxl
from openpyxl import Workbook
def check_team(team):
if team == "臺北富邦勇士Taipei Fubon Braves":
return 1
elif team == "福爾摩沙台新夢想家Formosa Taishin Dreamers":
return 4
elif team == "新竹街口攻城獅Hsinchu Jko Lioneers":
return 3
else:
return 2
excel_file = Workbook()
sheet = excel_file.active
excel2 = Workbook()
sheet2 = excel2.active
for k in range(61,70):
url="https://pleagueofficial.com/"
page = requests.get(url)
page = BeautifulSoup(page.text, "html.parser")
#---------------------日期 場管---------------------------------------------------
title = ["id","time","place"]
title_d = [k]
first = page.find("_ngcontent-uxb-c11",class_ = "mat-cell cdk-column-PlayerName mat-column-PlayerName ng-star-inserted").getText()
print(first)
quit()
del first[0]
print(first)
out_t = str(first[0])
title_d.append(first[0] + " "+ first[1])
t = first[3].split("\n")
num=0
for i in t:
if i != "":
title_d.append(i)
num+=1
if num ==2:
break
#print(title_d)
#------------------------客隊-------------------------------------------------------------
guest_s = []
team = page.find_all("div",class_ = "col-lg-4 col-4 align-self-center px-2")
guest = team[0]
#print(guest,type(guest))
img = guest.select("img")
img = (str(img[0]).replace("<img src=", "")).replace("/>", "")
#print(img)
guest = guest.getText().split("\n")
# print(guest)
for i in guest:
if i !="" and (not i.isdigit()):
guest_s.append(i)
team_id = check_team(guest_s[1])
guest_s.append(team_id)
guest_s.append(img)
del guest_s[0]
#print(guest_s,"\n\n\n")
#-----------------------------主隊------------------------------------------------------
home_s = []
home = team[1]
img = home.select("img")
img = (str(img[0]).replace("<img src=", "")).replace("/>", "")
#print(img)
home = home.getText().split("\n")
#print(home)
for i in home:
if i !="" and (not i.isdigit()):
home_s.append(i)
team_id = check_team(home_s[1])
home_s.append(team_id)
home_s.append(img)
del home_s[0]
#print(home_s)
#------------------------------分數-----------------------------------------------------
session = HTMLSession()
r=session.get(url,verify=False)
d = (r.html.find("tbody",first = True).text).split("\n")
del d[0]
#print(d)
h_t = 0
g_t = 0
for i in range(0,len(d),3):
guest_s.append(d[i])
home_s.append(d[i+2])
if d[i] != '-' and d[i+2] != '-':
h_t += int(d[i+2])
g_t+=int(d[i])
title = ["比賽id","time","場館","場次","隊名","主隊id","隊徽",'主隊Q1', '主隊Q2', '主隊Q3', '主隊Q4', '主隊OT', '主隊OT', '主隊Final',"隊名","客隊 id","隊徽",'客隊Q1', '客隊Q2', '客隊Q3', '客隊Q4', '客隊OT', '客隊OT', '客隊Final']
home_s.append(h_t)
guest_s.append(g_t)
print(home_s)
print(guest_s)
if k ==13:
sheet.append(title)
title_d += home_s + guest_s
print(title_d)
sheet.append(title_d)
#-----------------------------比賽個人數據-------------------------------------------------
json = "https://match.pleagueofficial.com/api/boxscore.php?id=" + str(k) + "&away_tab=total&home_tab=total"
r = requests.get(json, verify=False)
data = r.json()
#print(data)
data = data["data"]
print(type(data))
print(data,len(data),"\n\n\n\n")
home_list = data["home"]
away_list = data["away"]
t = list(home_list[0].keys())
key=["比賽id","team id","球員id","name_alt"]
del t[t.index("name_alt")]
del t[t.index("player_id")]
for i in t:
key.append(i)
print(key,"\n\n\n")
home=[]
away=[]
print(len(home_list),type(home_list))
for i in home_list:
t = [str(k)]
t.append(home_s[1])
t.append(i["player_id"])
t.append(i["name_alt"])
for x in range(4,len(key)):
j = key[x]
if j != "name_alt" and j != "player_id" :
if i[j] == '' or i[j] == None:
i[j] = "0"
if j=="seconds" and i[j] == "0":
i[j] = "DNP"
if "%" in str(i[j]):
i[j] = i[j].replace("%","")
if j == "starter":
if i[j] == "〇":
i[j] = 1
else:
i[j] = 0
#print(i[j],end = " ")
t.append(i[j])
print(t)
home.append(t)
print("\n")
print("\n\n\n\n")
print(len(away_list),type(away_list))
for i in away_list:
t=[str(k)]
t.append(guest_s[1])
t.append(i["player_id"])
t.append(i["name_alt"])
for x in range(4,len(key)):
j = key[x]
if j != "name_alt" and j != "player_id":
if i[j] == "" or i[j] == None:
i[j] = "0"
if j=="seconds" and i[j] == "0":
i[j] = "DNP"
if "%" in str(i[j]):
i[j] = i[j].replace("%","")
if j == "starter":
if i[j] == "〇":
i[j] = 1
else:
i[j] = 0
#print(i[j],end = " ")
t.append(i[j])
print(t)
away.append(t)
#-----------------------------------輸出----------------------------------------
if k==13:
sheet2.append(key)
for i in home:
sheet2.append(i)
for i in away:
sheet2.append(i)
out2 = "C:\\Users\\RB505\\Desktop\\hot\\比賽數據\\playoffs_球員數據.xlsx"
excel2.save(out2)
'''
out = "C:\\Users\\RB505\\Desktop\\hot\\比賽數據\\比賽資料.xlsx"
excel_file.save(out)
'''