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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# 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) |
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