Repository navigation
Expand file tree
/
Copy pathpython.py
More file actions
127 lines (111 loc) · 4.21 KB
/
Copy pathpython.py
File metadata and controls
127 lines (111 loc) · 4.21 KB
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
import requests
from bs4 import BeautifulSoup
import argparse
import json
import re
from pathlib import Path
SEASONS = {
"25-26": 1020,
"24-25": 979,
"23-24": 929,
"22-23": 858,
"21-22": 793,
"20-21": 780,
"19-20": 751,
"18-19": 693,
"17-18": 629,
"16-17": 557,
"15-16": 507,
"14-15": 449,
"13-14": 363,
"12-13": 307
}
BASE_URL = 'https://www.epsarg.gr/results/display_ranking.php?league_id={league_id}'
HEADERS = {
"User-Agent": "Mozilla/5.0 (compatible; RankingScraper/1.0; +https://example.com)"
}
def fetch_html(league_id: int) -> str:
url = BASE_URL.format(league_id=league_id)
resp = requests.get(url, headers=HEADERS, timeout=20)
resp.raise_for_status()
return resp.text
def parse_ranking(html: str):
soup = BeautifulSoup(html, "html.parser")
table = soup.select_one("table")
candidate_tables =[]
if table:
candidate_tables.append(table)
if not table:
for t in soup.find_all("table"):
rows = t.find_all("tr")
if any(len(r.find_all("td")) >= 3 for r in rows):
candidate_tables.append(t)
ranking_data = []
def extract_from_table(t):
rows = t.find_all("tr")
for row in rows:
if row.find("th"):
continue
cols = row.find_all("td")
if len(cols) < 3:
continue
position = cols[0].get_text(strip=True)
team_name = cols[1].get_text(strip=True)
points = cols[2].get_text(strip=True)
if not position or not team_name or not points:
continue
ranking_data.append({
"position": position,
"name": team_name,
"points": points
})
for t in candidate_tables:
extract_from_table(t)
if ranking_data:
break
if not ranking_data:
for row in soup.find_all("tr"):
cols = row.find_all("td")
if len(cols) >= 3:
position = cols[0].get_text(strip=True)
team_name = cols[1].get_text(strip=True)
points = cols[2].get_text(strip=True)
if position and team_name and points:
ranking_data.append({
"position": position,
"name": team_name,
"points": points
})
return ranking_data
def save_json(data, out_path: Path):
out_path.parent.mkdir(parents=True, exist_ok=True)
with out_path.open("w", encoding="utf-8") as f:
json.dump({"ranking": data}, f, ensure_ascii=False, indent=2)
def main():
parser = argparse.ArgumentParser(description="Λήψη βαθμολογίας EPS Arg για διαφορετικές χρονιές.")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--season", help="Χρονιά όπως '23-24' (θα μετατραπεί σε league_id από το mapping).")
group.add_argument("--league-id", type=int, help="Άμεση χρήση συγκεκριμένου league_id (π.χ. 929).")
parser.add_argument("--out", help="Όνομα αρχείου εξόδου (προαιρετικό). Αν δεν δοθεί, δημιουργείται αυτόματα.")
args = parser.parse_args()
if args.league_id is not None:
league_id = args.league_id
season_label = next((s for s, lid in SEASONS.items() if lid == league_id), f"lid-{league_id}")
else:
season = args.season.strip()
if season not in SEASONS:
raise SystemExit(
f"Η χρονιά '{season}' δεν υπάρχει στο mapping. "
f"Πρόσθεσέ την στο SEASONS ή χρησιμοποίησε --league-id."
)
league_id = SEASONS[season]
season_label = season
html = fetch_html(league_id)
ranking = parse_ranking(html)
# Όνομα αρχείου
out_name = args.out or f"ranking_{season_label}.json"
out_path = Path(out_name)
save_json(ranking, out_path)
print(f"Αποθηκεύτηκε: {out_path.resolve()} (εγγραφές: {len(ranking)})")
if __name__ == "__main__":
main()