import requests from bs4 import BeautifulSoup from services.standings_service import sync_standings from parsers.parser_sources import get_parser_source def fetch_html(url: str) -> str: headers = {"User-Agent": "Mozilla/5.0", "Accept": "*/*"} r = requests.get(url, headers=headers, timeout=20) r.raise_for_status() return r.text def parse_standings(html: str) -> list[dict]: soup = BeautifulSoup(html, "html.parser") standings: list[dict] = [] table_div = soup.find("ul", class_="custom-table__body") if not table_div: return standings rows = table_div.find_all("li", class_="custom-table__line") for row in rows: rank = row.find("div", class_="custom-table__number-wrapper").get_text(strip=True) team_name = row.find("div", class_="custom-table__team-name").get_text(strip=True) team_external_id = ( row.find("a", class_="custom-table__team custom-table__cell") .get("href") .split("?team_id=")[-1] ) vars_td = row.find_all("div", class_="custom-table__content")[1:] games = int(vars_td[0].get_text(strip=True)) wins = int(vars_td[1].get_text(strip=True)) draws = int(vars_td[2].get_text(strip=True)) losses = int(vars_td[3].get_text(strip=True)) plus_minus = vars_td[4].get_text(strip=True) gf = int(plus_minus.split("-")[0].strip()) ga = int(plus_minus.split("-")[1].strip()) points = int(vars_td[5].get_text(strip=True)) standings.append( { "team_external_id": str(team_external_id), "played": games, "wins": wins, "losses": losses, "draws": draws, "points_for": gf, "points_against": ga, "points": points, "position": int(rank), } ) return standings def run_parser_standings(source_key: str | None = None) -> None: source = get_parser_source(source_key) print(f"[parser_standings] Источник: {source['title']}") print(f"[parser_standings] URL: {source['standings_url']}") html = fetch_html(source["standings_url"]) standings_rows = parse_standings(html) if standings_rows: sync_standings( season=source["season"], standings_rows=standings_rows, ) print(f"[parser_standings] Synced rows: {len(standings_rows)}") else: print("[parser_standings] Строки турнирной таблицы не найдены") if __name__ == "__main__": run_parser_standings()