-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatp_men_singles_ranking.py
79 lines (63 loc) · 2.29 KB
/
atp_men_singles_ranking.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
# -*- coding: utf-8 -*-
import csv
from threading import Thread, Lock
from queue import Queue
import requests
from bs4 import BeautifulSoup
csv_file = open("atp_men_singles_ranking.csv", "w", newline='')
csv_writer = csv.DictWriter(csv_file, fieldnames=['date', 'rank', 'player', 'country', 'url', 'points', 'tournaments_played'])
def get_dates():
response = requests.get("http://www.atpworldtour.com/Rankings/Singles.aspx")
soup = BeautifulSoup(response.text)
options = soup.find(id="singlesDates").find_all("option")
return [option.text for option in options]
def parse_rankings(html_page):
soup = BeautifulSoup(html_page)
atp_ranking_table = soup.find("table", class_="bioTableAlt")
atp_ranking_tr_all = atp_ranking_table.find_all("tr")[1:] if atp_ranking_table else []
for atp_ranking_tr in atp_ranking_tr_all:
split_text = atp_ranking_tr.find("td").text.strip().split()
rank, player, country = split_text[0], " ".join(split_text[1:-1]), split_text[-1][1:-1]
url = atp_ranking_tr.find("a").get('href')
points = atp_ranking_tr.find_all("td")[1].text
tournaments_played = atp_ranking_tr.find_all("td")[3].text
yield {
'rank': rank,
'player': player,
'country': country,
'url': url,
'points': points,
'tournaments_played': tournaments_played
}
def get_rankings(params):
try:
response = requests.get("http://www.atpworldtour.com/Rankings/Singles.aspx", params=params)
if response.status_code != 200:
print(response.status_code, params)
return False
print("ok")
for ranking in parse_rankings(response.text):
ranking['date'] = params['d']
with Lock():
csv_writer.writerow(ranking)
return True
except Exception as e:
print(e)
return False
def worker():
while True:
params = q.get()
if not get_rankings(params):
q.put(params)
q.task_done()
print(q.qsize())
q = Queue()
for i in range(6):
t = Thread(target=worker)
t.daemon = True
t.start()
atp_singles_dates = get_dates()
for r in ("1", "101", "201", "301", "401"):
for d in atp_singles_dates:
q.put({"r": r, "d": d})
q.join()