Quick Start

Quick Start

This guide will get you up and running with the SwimRankings library in minutes.

Basic Search

The most common use case is searching for athletes by last name:

from swimrankings import Athletes
 
# Search for athletes named "Druwel"
athletes = Athletes(name="Druwel")
 
# Print all found athletes
for athlete in athletes:
    print(f"{athlete.full_name} ({athlete.birth_year}) - {athlete.country}")
    print(f"Club: {athlete.club}")
    print(f"Profile: {athlete.profile_url}")
    print()

Example output:

DRUWEL, Mauro (2008) - BEL
Club: BEL - TiMe Swimming Team
Profile: https://www.swimrankings.net/index.php?page=athleteDetail&athleteId=5199475

DRUWEL, Nora (2011) - BEL
Club: BEL - TiMe Swimming Team
Profile: https://www.swimrankings.net/index.php?page=athleteDetail&athleteId=5425825

Search Options

Athletes(name, gender="all", club_id=-1, timeout=30, auth=None) accepts a last name, a gender filter ("all", "male", or "female"), a club ID (-1 for all clubs), a request timeout in seconds, and optional (email, password) credentials:

from swimrankings import Athletes
 
athletes = Athletes(
    name="Smith",
    gender="male",
    club_id=123,
    timeout=30,
    auth=("[email protected]", "your_password"),
)

Authenticating & Getting Athlete Details

Most athlete detail pages on swimrankings.net return HTTP 401 when requested anonymously. Authenticate with a free Swimrankings Member account before calling get_details(), or pass auth=(email, password) directly.

import swimrankings
from swimrankings import Athletes, AuthenticationError
 
# 1. Authenticate once for the current Python process
swimrankings.login("[email protected]", "your_password")
 
# 2. Search and fetch detailed personal bests
athletes = Athletes(name="Druwel")
athlete = athletes[0]
 
try:
    details = athlete.get_details()
    print(f"Details for {athlete.full_name} (updated {details.last_updated:%Y-%m-%d})")
    for pb in details.personal_bests:
        print(f"  {pb.event} ({pb.course}): {pb.time} - {pb.date or 'N/A'}")
except AuthenticationError as exc:
    print(f"Authentication failed: {exc}")

Inspecting profile metadata returned in details:

if details.profile_info:
    print("\nProfile Information:")
    for key, value in details.profile_info.items():
        print(f"  {key.title()}: {value}")

Gender-Specific Searches

You can filter athletes by gender at query time:

from swimrankings import Athletes
 
male_athletes = Athletes(name="Smith", gender="male")
female_athletes = Athletes(name="Smith", gender="female")
all_athletes = Athletes(name="Smith", gender="all")
 
print(f"Male: {len(male_athletes)}, Female: {len(female_athletes)}, Total: {len(all_athletes)}")

Working with Results

Athletes implements Python's sequence and iterator protocols:

athletes = Athletes(name="Johnson")
 
# Iterate
for athlete in athletes:
    print(athlete.full_name)
 
# Index and slice
if athletes:
    first_athlete = athletes[0]
    last_athlete = athletes[-1]
    first_three = athletes[0:3]
    print(f"Found {len(athletes)} athletes; top 3: {[a.full_name for a in first_three]}")

Filtering Results

Apply in-memory filters after a search without making extra HTTP requests:

athletes = Athletes(name="Miller")
 
usa_athletes = athletes.filter_by_country("USA")
young_athletes = athletes.filter_by_birth_year(2005, 2015)
born_2008 = athletes.filter_by_birth_year(2008)
male_athletes = athletes.filter_by_gender("male")

Accessing Athlete Data

Each Athlete exposes parsed summary fields:

athlete = Athletes(name="Druwel")[0]
 
print(f"ID: {athlete.athlete_id}")
print(f"Full name: {athlete.full_name}")
print(f"First name: {athlete.first_name}")
print(f"Last name: {athlete.last_name}")
print(f"Birth year: {athlete.birth_year}")
print(f"Gender: {athlete.gender}")
print(f"Country: {athlete.country}")
print(f"Club: {athlete.club}")
print(f"Profile URL: {athlete.profile_url}")

Error Handling

Catch AuthenticationError before NetworkError (since AuthenticationError subclasses NetworkError):

from swimrankings import Athletes, AthleteNotFoundError, AuthenticationError, NetworkError
 
try:
    athletes = Athletes(name="NonexistentName")
    details = athletes[0].get_details()
except AthleteNotFoundError:
    print("No athletes found with that name")
except AuthenticationError:
    print("Member login required or credentials rejected")
except NetworkError as exc:
    print(f"Network error: {exc}")

Data Export

Convert search results to plain dictionaries for JSON, CSV, or Pandas:

import json
from swimrankings import Athletes
 
athletes_dict = Athletes(name="Smith").to_dict()
with open("athletes.json", "w", encoding="utf-8") as f:
    json.dump(athletes_dict, f, indent=2, ensure_ascii=False)

Next Steps