-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathATPRankCrawler.py
56 lines (39 loc) · 1.31 KB
/
ATPRankCrawler.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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 21 10:50:14 2014
@author: Gaspard
"""
from playerCrawler import *
from utils import *
def intFromRank(r):
if r[-1] == 'T':
r = r[:-1]
r = "".join(r.split(","))
r = "".join(r.split("."))
return int(r)
class ATPRank:
def __init__(self, fs):
self.rankPath = fs.ranksFolder
def addATPRank(self, dico):
code = dico['IDPlayer']
ID = dico['ID']
path = self.getPath(ID)
if self.isTreated(path):
return False
else:
url = urlOpen( url_players + code + '.aspx?t=rh' )
dom = BeautifulSoup(url)
trList = dom.find('table').find_all('tr')
with open(path+'2', 'wb') as f:
w = csv.DictWriter(f, [ 'Time', 'Rank' ])
for tr in trList[1:]:
tab = tr.find_all('td')
if len( tab[1].contents ) > 0:
w.writerow( {'Time' : tab[0].contents[0],
'Rank' : intFromRank( tab[1].contents[0] ) } )
os.rename( path+"2", path )
return True
def getPath(self, ID):
return self.rankPath + "p" + str(ID) + "p.csv"
def isTreated(self, path):
return os.path.isfile(path)