Python 3 Web Scraping Airlines API Script to Get Flight Status Using BeautifulSoup4 and JSON Module Full Project For Beginners

Python 3 Web Scraping Airlines API Script to Get Flight Status Using BeautifulSoup4 and JSON Module Full Project For Beginners

 

Welcome folks today in this blog post we will be scraping airlines api to get flight status using beautifulsoup4 and json module in python. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started we need to install the following library using the pip command as shown below

 

pip install bs4

 

pip install requests

 

After installing this library you need to make an app.py file and copy paste the following code

 

app.py

 

# import module 
import requests 
from bs4 import BeautifulSoup 

# UDF for get HTML code 
# from URL 


def get_html(Airline_code, Flight_number, Date, Month, Year): 
    def getdata(url): 
        r = requests.get(url) 
        return r.text 

    # url 
    url = "https://www.flightstats.com/v2/flight-tracker/"+Airline_code + "/"+Flight_number+"?year="+Year+"&month="+Month+"&date="+Date 

    # pass the url 
    # into getdata function 
    htmldata = getdata(url) 
    soup = BeautifulSoup(htmldata, 'html.parser') 
    return(soup) 

# Get Flight number 
# from Html code 


def flight_no(soup): 
    Flight_no = "" 

    # Find div tag with 
    # unique class name 
    for i in soup.find("div", class_="ticket__FlightNumberContainer-s1rrbl5o-4 hgbvHg"): 
        Flight_no = Flight_no + (i.get_text()) + " "
    return (Flight_no) 

# Get Airport name 
# from HTML code 


def airport(soup): 
    Airport_name = [] 
    # Find div tag with 
    # unique class name 
    for i in soup.find_all("div", class_="text-helper__TextHelper-s8bko4a-0 CPamx"): 
        Airport_name.append(i.get_text()) 
    return (Airport_name) 

# get status 
# from HTML code 


def status(soup, Airport_list): 
    Time_status = [] 
    Airport_List = [] 
    Status_str = [] 
    Gate = [] 
    Gate_no = [] 

    # Find div tag with 
    # unique class name 
    # to get Gate number 
    for data in soup.find_all("div", class_="ticket__TGBLabel-s1rrbl5o-15 gcbyEH text-helper__TextHelper-s8bko4a-0 dfeqpK"): 
        Gate.append(data.get_text()) 
    for data in soup.find_all("div", class_="ticket__TGBValue-s1rrbl5o-16 icyRae text-helper__TextHelper-s8bko4a-0 cCfBRT"): 
        Gate_no.append(data.get_text()) 

    # Get status from 
    # html code 
    for i in soup.find_all("div", class_="text-helper__TextHelper-s8bko4a-0 bcmzUJ"): 
        Status_str.append(i.get_text()) 
    for i in soup.find_all("div", class_="text-helper__TextHelper-s8bko4a-0 cCfBRT"): 
        Time_status.append(i.get_text()) 

    # traverse the Data 
    # from scarping data 
    for item in range(4): 
        if item == 0: 
            print(Airport_list[0]) 
        if item == 2: 
            print("") 
            print(Airport_list[1]) 
        print(Status_str[item] + " : " + Time_status[item]) 
        print(Gate[item] + " : " + Gate_no[item]) 
    for item in range(len(Gate)): 
        print(Gate[item] + " : " + Gate_no[item]) 


# Driver code 
if __name__ == '__main__': 
    # Input Data from geek 
    Airline_code = 'G8'
    Flight_number = '134'
    Date = '23'
    Month = '10'
    Year = '2020'

    # Calling the get_html 
    # with argument 
    # function calling 
    soup = get_html(Airline_code, Flight_number, Date, Month, Year) 
    print("Flight number : ", flight_no(soup)) 
    Airport_list = airport(soup) 
    status(soup, Airport_list)

 

See also  Python 3 Tkinter College or School Fee Management System Using MySQL Database GUI Desktop App Full Project For Beginners

 

Here in the above python script you need to replace the following parameters

 

flight_number

date

month

year

airline number

 

Now after replacing all this information which is listed above you need to run the python script by typing the below command as shown below

 

python app.py

Leave a Reply