
Untitled
Guest 80 13th Jan, 2025
import requests
import pandas as pd
# Funkcja do pobierania danych meczowych z API
def fetch_match_data(season):
url = f"https://api.openligadb.de/getmatchdata/bl1/{season}"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
print(f"Błąd w zapytaniu dla sezonu {season}: {response.status_code}")
return []
# Funkcja do wyciągania danych i formatowania
def process_match_data(matches):
data = []
for match in matches:
match_id = match.get("MatchID")
league_id = match.get("LeagueID")
league_season = match.get("LeagueSeason")
team1 = match.get("Team1", {}).get("TeamName")
team2 = match.get("Team2", {}).get("TeamName")
# Wyniki meczu
match_results = match.get("MatchResults", [])
points_team1 = None
points_team2 = None
if match_results:
points_team1 = match_results[0].get("PointsTeam1")
points_team2 = match_results[0].get("PointsTeam2")
# Gole
goals = match.get("Goals", [])
goal_data = "; ".join([f"{goal['Minute']}': {goal['GoalGetterName']} ({goal['Team1']})" for goal in goals])
# Lokalizacja
location = match.get("Location", {}).get("LocationCity")
# Dodanie danych do listy
data.append({
"MatchID": match_id,
"LeagueID": league_id,
"LeagueSeason": league_season,
"Team1": team1,
"Team2": team2,
"PointsTeam1": points_team1,
"PointsTeam2": points_team2,
"Goals": goal_data,
"Location": location
})
return data
# Pobranie danych dla wybranych sezonów
seasons = [2022, 2023, 2024]
all_matches = []
for season in seasons:
matches = fetch_match_data(season)
processed_data = process_match_data(matches)
all_matches.extend(processed_data)
# Konwersja do DataFrame i zapis do CSV
df = pd.DataFrame(all_matches)
df.to_csv("bundesliga_matches.csv", index=False)
print("Dane zapisane do pliku bundesliga_matches.csv")
To share this paste please copy this url and send to your friends
RAW Paste Data