-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_crawler.py
174 lines (162 loc) · 6.99 KB
/
player_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
import requests
import time
from BeautifulSoup import BeautifulSoup
import re
import pickle
from time import sleep
datafile=open("PlayerFile.txt","r+b")
playerdata=[dict() for x in range(1190)]
for i in xrange(1185):
x=pickle.load(datafile)
#print x
if(x):
playerdata[i]=x
i+=1
goalie_data=open("goalie_data.csv","a")
outfield_data=open("outfield_data.csv","a")
goalie_data.write("Player Name,Club,Position,Appearances,Squawka Score,Cleansheets,Goals Conceded,POTM,Saves Per Game,Saves Per Goal,Average Claim Success,Average Punch Accuracy,Distribution Success,Average Distribution Length,Yellow Cards,Red Cards,Points\n")
outfield_data.write("Player Name,Club,Position,Appearances,Squawka Score,Goals Scored,Shot Accuracy,POTM,Chances Created,Average Pass Accuracy,Average Pass Length,Duels Won,Average Defensive Actions,Defensive Errors,Yellow Cards,Red Cards,Points\n")
goalie_data.close()
outfield_data.close()
goalie_stat_dict={"App":0,"Perf":0,"CS":0,"GC":0,"SqA":0,"SPGame":0,"SPGoal":0,"ACS":0,"AP":0,"DS":0,"DL":0,"Y":0,"R":0}
goalie_stat_dict_ref={1:"App",2:"Perf",3:"CS",4:"GC",5:"SqA",6:"SPGame",7:"SPGoal",8:"ACS",9:"AP",10:"DS",11:"DL",12:"YR"}
goalie_stat_dict_ref_file={1:"App",2:"Perf",3:"CS",4:"GC",5:"SqA",6:"SPGame",7:"SPGoal",8:"ACS",9:"AP",10:"DS",11:"DL",12:"Y",13:"R"}
def crawl(url):
while True:
try:
print "trying to get URL... ",
r=requests.get(url)
print "Got URL!"
return r.content
break
except Exception as e:
print e
sleep(2)
print "Retrying!!"
base_url = 'http://www.squawka.com/players/'
def urlify(player,base_url):
print "Parsing URL..",
return base_url+player+'/stats'
with open ("path.txt", "r") as myfile:
data=myfile.read()
check=open("players_done.txt","r")
donelist=check.readlines()
check.close()
#print "Done players are:"
#print donelist
for i in xrange(len(playerdata)):
stat_dict={"App":0,"Perf":0,"GS":0,"ShtA":0,"SqA":0,"CC":0,"APA":0,"APL":0,"DW":0,"ADA":0,"DE":0,"Y":0,"R":0}
stat_dict_ref={1:"App",2:"Perf",3:"GS",4:"ShtA",5:"SqA",6:"CC",7:"APA",8:"APL",9:"DW",10:"ADA",11:"DE",12:"YR"}
stat_dict_ref_file={1:"App",2:"Perf",3:"GS",4:"ShtA",5:"SqA",6:"CC",7:"APA",8:"APL",9:"DW",10:"ADA",11:"DE",12:"Y",13:"R"}
player=playerdata[i]["slug"]
playername=playerdata[i]["name"]
player=player.replace(" ","-")
#print i
print "Fetching information for %s "%playerdata[i]['name']+"from "+str(urlify(player,base_url))
if(len(donelist)>i):
if(donelist[i].strip()==player):
print "Statistics for this player has already been fetched. Moving on.."
continue
print "waiting..."
#sleep(4)
print "Sleep over!"
html = crawl(urlify(player,base_url))
print "Page crawled."
parsed_html = BeautifulSoup(html)
#print parsed_html.select(data)
print ""
position=parsed_html.find("h2", { "class" : "team-league" })
start=">"
end="</h2>"
position=re.search('%s(.*)%s' % (start, end),str(position)).group(1)
player_name_parsed=parsed_html.find("h1", { "class" : "team-name" })
start=">"
end="</h1>"
player_name_parsed=re.search('%s(.*)%s' % (start, end),str(player_name_parsed)).group(1)
club=parsed_html.find("span", { "id" : "player_club_name" })
start=">"
end="</span>"
club=re.search('%s(.*)%s' % (start, end),str(club)).group(1)
if (position=="Goalkeeper"):
goalie_stat_dict={"App":0,"Perf":0,"CS":0,"GC":0,"SqA":0,"SPGame":0,"SPGoal":0,"ACS":0,"AP":0,"DS":0,"DL":0,"Y":0,"R":0}
goalie_stat_dict_ref={1:"App",2:"Perf",3:"CS",4:"GC",5:"SqA",6:"SPGame",7:"SPGoal",8:"ACS",9:"AP",10:"DS",11:"DL",12:"YR"}
for i in xrange(1,13):
#print str(i)+":"
value = parsed_html.body.find(id='stat-'+str(i)).find('span',attrs={'class':'stat'}).text
heading = parsed_html.body.find(id='stat-'+str(i)).find('span',attrs={'class':'heading'}).text
#print heading+' '+value
if(i==12 or i==5 or i==4 or i==6 or i==7):
#print int(value.split('/')[0])
#print int(value.split('/')[1])
if(i==12):
goalie_stat_dict["Y"]=int(value.split('/')[0])
stat_dict["R"]=int(value.split('/')[1])
if(i==4 or i==6 or i==7):
goalie_stat_dict[goalie_stat_dict_ref[i]]=float(value)
#print goalie_stat_dict[goalie_stat_dict_ref[i]]
else:
value=re.sub('[^0-9]','', value)
goalie_stat_dict[goalie_stat_dict_ref[i]]=int(value)
#print goalie_stat_dict[goalie_stat_dict_ref[i]]
#print goalie_stat_dict
points=goalie_stat_dict["App"]*2+goalie_stat_dict["SPGame"]*20+goalie_stat_dict["CS"]*4-goalie_stat_dict["Y"]-goalie_stat_dict["R"]*3
print playername+" %.2f"%points
file1=open("statfile.txt","a")
file1.write(str(playername+": %.2f"%points+"\n"))
file1.close()
file1=open("players_done.txt","a")
file1.write(str(player+"\n"))
file1.close()
goalie_data=open("goalie_data.csv","a")
goalie_data.write(str(player_name_parsed)+",")
goalie_data.write(str(club)+",")
goalie_data.write(str(position)+",")
#print goalie_stat_dict[goalie_stat_dict_ref_file[1]]
#print goalie_stat_dict[goalie_stat_dict_ref_file[2]]
for u in xrange(1,14):
goalie_data.write(str(str(goalie_stat_dict[goalie_stat_dict_ref_file[u]])+","))
goalie_data.write(str(points)+",")
goalie_data.write("\n")
goalie_data.close()
continue
for i in xrange(1,13):
value = parsed_html.body.find(id='stat-'+str(i)).find('span',attrs={'class':'stat'}).text
heading = parsed_html.body.find(id='stat-'+str(i)).find('span',attrs={'class':'heading'}).text
#print heading+' '+value
if(i==12 or i==5):
#print int(value.split('/')[0])
#print int(value.split('/')[1])
if(i==12):
stat_dict["Y"]=int(value.split('/')[0])
stat_dict["R"]=int(value.split('/')[1])
else:
value=re.sub('[^0-9]','', value)
stat_dict[stat_dict_ref[i]]=int(value)
#print stat_dict[stat_dict_ref[i]]
if(position=="Defender"):
points=stat_dict["App"]*2+stat_dict["GS"]*6+stat_dict["CC"]*1.25+(stat_dict["DW"]*stat_dict["App"])/15+stat_dict["ADA"]*3-stat_dict["R"]*3-stat_dict["Y"]
print playername+": %.2f"%points
if(position=="Midfielder"):
points=stat_dict["App"]*2+stat_dict["GS"]*5+stat_dict["CC"]+((stat_dict["DW"]+stat_dict["APA"])*stat_dict["App"])/40+stat_dict["ADA"]*6-stat_dict["R"]*3-stat_dict["Y"]
print playername+": %.2f"%points
if(position=="Forward"):
points=stat_dict["App"]*2+stat_dict["GS"]*4+stat_dict["CC"]*1.25+((stat_dict["ShtA"]+stat_dict["APA"])*stat_dict["App"])/40-stat_dict["R"]*3-stat_dict["Y"]
print playername+": %.2f"%points
file1=open("statfile.txt","a")
file1.write(str(playername+": %.2f"%points+"\n"))
file1.close()
file1=open("players_done.txt","a")
file1.write(str(player+"\n"))
file1.close()
outfield_data=open("outfield_data.csv","a")
outfield_data.write(str(player_name_parsed)+",")
outfield_data.write(str(club)+",")
outfield_data.write(str(position)+",")
for u in xrange(1,14):
outfield_data.write(str(str(stat_dict[stat_dict_ref_file[u]])+","))
outfield_data.write(str(points)+",")
outfield_data.write("\n")
outfield_data.close()
print ""
print ""
#print stat_dict