-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpandas_html2.py
70 lines (49 loc) · 1.92 KB
/
pandas_html2.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
# https://jakevdp.github.io/PythonDataScienceHandbook/index.html
# Needs html5lib `pip install html5lib`
import datetime
import pandas as pd
import requests
import matplotlib.pyplot as plt
# Tarefa: Get data for the top 20 for the past 10 weeks and plot?
headers = {'User-Agent': ''}
def get_past_weeks(ipt, n=7):
# Transforme input em objeto datetime
ipt = datetime.datetime.strptime(ipt, '%Y-%m-%d')
# Make a list of past dates
return [ipt - datetime.timedelta(days=7 * i) for i in range(n)]
def grouping(data, by, value):
print(data.groupby(by).agg('mean')[value])
def get_table(path):
rank = requests.get(path, headers=headers)
rank = pd.read_html(rank.text)
return rank[0]
def get_many_tables(start_day='2022-03-21'):
out = pd.DataFrame()
dates = get_past_weeks(start_day)
for d in dates:
# strftime: transforma objeto datetime em texto
p2 = f"https://www.atptour.com/en/rankings/singles?rankRange=1-10&" \
f"rankDate={datetime.datetime.strftime(d, '%Y-%m-%d')}"
print(p2)
t = get_table(p2)
t = t[['Rank', 'Player', 'Points']]
t['week'] = datetime.datetime.strftime(d, '%Y-%m-%d')
out = out.append(t)
return out
def plot_shinfrin(table):
for each in table['Player'].unique():
tab = table[table['Player'] == each]
tab = tab.sort_values('week')
plt.plot(tab['week'], tab['Points'], label=each)
plt.text(tab.reset_index(drop=True).loc[len(tab) - 2]['week'],
tab.reset_index(drop=True).loc[len(tab) - 2]['Points'], each)
# plt.legend(frameon=False)
plt.show()
if __name__ == '__main__':
p = 'https://www.atptour.com/en/rankings/singles?rankRange=1-5000&rankDate=2022-03-21'
r = get_table(p)
# r.loc[r.Age == r.Age.max()]['Player']
# r.loc[r.Age == r.Age.min()]['Player']
# len(r.loc[r.Age == 15])
ts = get_many_tables()
plot_shinfrin(ts)