Get Flight Status using Python


"Flight status" refers to the present condition of a flight, such as whether it is on schedule, running behind schedule, or being cancelled. You may find out the status of a flight by visiting the airline's website and entering the flight number or the airports of departure and arrival. The essential data is then taken from the HTML page and structured by the BeautifulSoup module and used to assess if a flight is on time, delayed, or cancelled. To obtain the flight status for this blog post, we'll utilise Python.

Installation

It's essential that Python and the BeautifulSoup library be set up on your machine before we start so fire up your terminal and use pip to get it done.

pip install requests
pip install beautifulsoup4

Algorithm

Import necessary libraries − The required libraries for this program are 'requests', 'BeautifulSoup', and 'datetime'.

  • Define the 'get_flight_details' function that takes in 'airline_code', 'flight_number', 'date', 'month', and 'year' as input parameters.

  • Define 'get_data' helper function that retrieves HTML data from a URL.

  • Construct a URL using input parameters and FlightStats website format.

  • Retrieve HTML data using 'get_data' helper function and URL.

  • Parse HTML data using BeautifulSoup and return the parsed HTML data.

  • Define 'get_airport_names' function that takes in parsed HTML data as input.

  • Retrieve airport names from parsed HTML data and print flight number, flight name, from and to airports.

  • Define 'get_flight_status' function that takes in parsed HTML data as input.

  • Retrieve gate numbers, status, and time status details from parsed HTML data and print them.

  • Define input parameters for airline code, flight number, current date, date, month, and year.

  • Call 'get_flight_details' function with input parameters to retrieve parsed HTML data.

  • Call 'get_airport_names' function with parsed HTML data to retrieve and print airport names.

  • Call 'get_flight_status' function with parsed HTML data to retrieve and print flight status details.

Note that we are going to grab the elements highlighted in the image below using CSS selectors and find() function provisioned by BeautifulSoup.

Example

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)
soup = get_flight_details(airline_code, flight_number, date, month, year)
get_airport_names(soup)
get_flight_status(soup)

Output

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

The BeautifulSoup module parses the HTML data and collects flight information from the FlightStats website based on the input parameters. The script specifies get_flight_details(), get_airport_names(), and get_flight_status() as its two major functions. Using the input parameters, the get_flight_details() function creates a URL, uses the requests library to send a GET request to that URL, and then retrieves the HTML data from the response. The HTML data is then parsed using BeautifulSoup, and the processed HTML data is returned.

Using BeautifulSoup, the get airport_names() function pulls the airport names from the parsed HTML data. The flight number, flight name, departure airport name, arrival airport name, and airport code are then printed. The parsed HTML data is likewise ingested by the get_flight_status() method, which then makes use of BeautifulSoup to retrieve the flight status information. The gate number, current flight status, departure time, and arrival time are then printed. The get_flight_details() method is used by the main script to receive the parsed HTML data when the input parameters, including the airline code, flight number, day, month, and year, have been initialized. To extract and output the airport names and flight status information, respectively, it then calls the get_airport_names() and get_flight_status() methods.

Applications

Passengers, airline workers, and data analysts all gain advantages from having real-time access to flight data. It is possible to automate the process of gathering flight information from a number of websites, making it faster and more efficient. This can be accomplished with the assistance of Python and BeautifulSoup. Travellers will appreciate being able to check the status of their flight without having to call the airline or visit numerous websites. Personnel, flight scheduling, and other decisions will be made by knowledgeable airline professionals. Examiners of information can take a gander at examples and patterns in air travel to anticipate future interest and limit needs as well as distinguish open doors for productivity and cost-cutting drives.

Conclusion

The subject of web scratching and how it attempts to extricate information from sites was shrouded in this blog entry. It covered the utilisation of Python libraries like BeautifulSoup and Solicitations as well as the essentials of web scratching, including HTML, CSS, and the Report Article Model (DOM). After that, a straightforward Python script was used to extract flight information from the FlightStats website, and a step-by-step tutorial on web scraping was provided. It is essential to be aware of legal and ethical considerations when web scraping, such as adhering to a website's terms of service and avoiding excessive data requests that could overload its servers.

Updated on: 18-Jul-2023

415 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements