Get Flight Status using Python

Flight status refers to the current condition of a flight, indicating whether it is on schedule, delayed, or cancelled. Python provides powerful tools to extract flight information from airline websites using web scraping techniques with BeautifulSoup and requests libraries.

Installation

Before starting, ensure Python and the required libraries are installed on your system

pip install requests
pip install beautifulsoup4

Algorithm Overview

The flight status retrieval process follows these key steps

  • Import necessary libraries: requests, BeautifulSoup, and datetime

  • Define the get_flight_details() function to construct URL and fetch HTML data

  • Parse HTML data using BeautifulSoup to extract flight information

  • Extract airport names, flight numbers, and status details using CSS selectors

  • Display formatted flight information including gates, status, and timings

Implementation

Here's the complete Python script to get flight status

import requests
from bs4 import BeautifulSoup
from datetime import datetime

def get_flight_details(airline_code, flight_number, date, month, year):
    def get_data(url):
        response = requests.get(url)
        return response.text

    # Construct URL using input parameters
    url = f"https://www.flightstats.com/v2/flight-tracker/{airline_code}/{flight_number}?year={year}&month={month}&date={date}"

    # Get HTML data from URL
    html_data = get_data(url)

    # Parse HTML using BeautifulSoup
    soup = BeautifulSoup(html_data, 'html.parser')

    # Return parsed HTML data
    return soup

def get_airport_names(soup):
    airport_names = [
        i.get_text()
        for i in soup.find_all(
            "div", class_="text-helper__TextHelper-sc-8bko4a-0"
        )
    ]
    print("Flight No:", airport_names[0])
    print("Flight Name:", airport_names[1])
    print("FROM:", airport_names[2], airport_names[3])
    print("TO:", airport_names[4], airport_names[5])

def get_flight_status(soup):
    gates = [
        data.get_text()
        for data in soup.find_all(
            "div",
            class_="ticket__TGBLabel-s1rrbl5o-15 gcbyEH text-helper__TextHelper-sc-8bko4a-0 efwouT",
        )
    ]
    gate_numbers = [
        data.get_text()
        for data in soup.find_all(
            "div",
            class_="ticket__TGBValue-sc-1rrbl5o-16 hUgYLc text-helper__TextHelper-sc-8bko4a-0 kbHzdx",
        )
    ]
    statuses = [
        i.get_text()
        for i in soup.find_all(
            "div", class_="text-helper__TextHelper-sc-8bko4a-0 feVjck"
        )
    ]
    time_statuses = [
        i.get_text()
        for i in soup.find_all(
            "div", class_="text-helper__TextHelper-sc-8bko4a-0 kbHzdx"
        )
    ]

    print("Gate No: ", gate_numbers[0])
    print("Status: ", statuses[0])
    print(f"FROM: {time_statuses[0]} | TO: {time_statuses[2]}")

# Input parameters
airline_code = 'AA'
flight_number = '1'
current_date = datetime.now()
date = str(current_date.day+1)
month = str(current_date.month)
year = str(current_date.year)

# Get flight details and display information
soup = get_flight_details(airline_code, flight_number, date, month, year)
get_airport_names(soup)
get_flight_status(soup)

The output displays comprehensive flight information

Flight No: AA 1
Flight Name: American Airlines
FROM: JFK New York
TO: LAX Los Angeles
Gate No:  8
Status:  On time
FROM: 07:15 EDT | TO: 10:35 PDT

How It Works

The script uses BeautifulSoup to parse HTML data from FlightStats website. The get_flight_details() function constructs a URL using input parameters, sends a GET request using the requests library, and returns parsed HTML data.

The get_airport_names() function extracts airport information using CSS selectors, while get_flight_status() retrieves gate numbers, flight status, and timing information from the parsed HTML.

Key Applications

  • Passenger convenience Check flight status without visiting multiple websites

  • Airline operations Automated flight monitoring for staff and scheduling decisions

  • Data analysis Analyze flight patterns and trends for business insights

  • Travel applications Integrate real-time flight data into mobile apps

Important Considerations

When implementing web scraping for flight data, consider these factors

  • Respect website's terms of service and robots.txt file

  • Implement proper error handling for network requests

  • Add delays between requests to avoid overloading servers

  • CSS selectors may change when websites update their structure

Conclusion

Python's BeautifulSoup and requests libraries provide an effective solution for extracting flight status information from airline websites. This approach enables automated flight monitoring while respecting ethical web scraping practices and website terms of service.

Updated on: 2026-03-27T08:38:11+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements