Easy and fast flight price monitor with python

Jeferson Machado Santos
5 min readJul 28, 2020

I love to travel, and I believe more and more people do. However, since I am not still rich enough to go anywhere anytime I want, always when I start to plan a new trip the search for a flight with the price we are willing to pay can become tedious and frustrating. So, I decided to build this flight price monitor with python and keep it alive looking for flights always that I want one, and send me a warning by e-mail when it finds one with the price I believe is fair enough. You can find the complete code for this solution on my github, here.

First of all, you can start a new python file, like “flight_monitor.py”. I use to write code with Visual Studio Code, but you can use whatever software you prefer. Then we start our code importing all the libraries which we will need to develop our project.

We will use webdriver and beautifulSoup to access the website and web scrap the data of the flights. Time will be used to pause the code and wait for the website to load flight prices. Also, email.mime and smtplib will be used to send us an email every time a flight within our criteria is found. Lastly, os, dotenv and pathlib will be used to keep our e-mail addresses and password safe and not show them in our main code.

Before we continue with the code, let’s talk about where we will find our flights. I chose Kayak.com, because I use to find better flights and its interface is good as well. Anyway, the idea of this code should work with any website that you choose. We will make a search with origin and destination we want, as well as dates. After the results are shown, we still can adjust it with maximum stops, price range, etc. After it, we can copy the URL to use in our code. Every time this URL is accessed, the search will be updated with most recent flights data. By the way, my search is from São Paulo (GRU) to Frankfurt (FRA), by the end of 2020. Now we can go back to our code.

After importing the libraries, we start web scrapping the data on our search. Lines 1 until 6 open the URL of our search on Google Chrome, wait for it to load and then copy all the content of the page to a variable ‘soup’. With the html content of the page on this variable, we can find in this content the information of the lowest price found, that is shown on top of the search. As you can see on the print below, exploring the html code in Google Chrome, this part of the html structure has a class attribute (from CSS syntax, for those not familiar) called ‘js-label js-price _itL _ibU _ibV _idj _kKW’. So in lines 8 and 9 we find the section of the code with this class and store the first 4 character of its text on a variable price.

Now that we have the lowest price of our search in the variable ‘price’, we can check if it matches what we are willing to pay, and then email us the result.

In this case, I am willing to pay until BRL 3.500 (I am willing to pay less, but I changed it here so we can see it working), so line 1 will check if the value on variable price is smaller than 3.500. It is important to transform the variable price to integer with ‘int(price)’, since it originally is a string, because it was extracted from the text of the page. On lines 8 until 15 we access a .env file that should be created on the same directory of our python file and then we use the variables value for password, from and to that are stored there. For more information about how to work with .env file and python, please check this link. We are making this so it is not necessary to write our passwords and other sensitive data in our code, and we can keep the .env file out of public repositories for safety. In my case, I will be sending the message from a gmail e-mail I created only for this purpose, to my daily outlook e-mail, so If a flight is found, it will be on my daily e-mails. On next lines, we define the subject and the message that will be on the body of the e-mail with the price and search URL, as well as send the message with the smtp server created on line 22. Finally, we can close the Google Chrome tab opened to search the prices with ‘driver.quit()’.

Below you can see an example of how the e-mail will be received when a flight within the price desired is found.

To keep this alive and running every time, I created an AWS Windows Instance and saved my python code there. Also, I added an while loop and all my code went inside it. The idea is that this while loop never ends, so my code will keep searching for flights. I also added a time sleep of 7200 seconds, which are 2 hours, in the end of the loop, so the search will occur every 2 hours.

So, I hope this flight monitor help me find a cheap ticket to Germany and I hope it can help you with finding new flights or learning and developing new ideas. Please, comment and share any of your thoughts.

--

--

Jeferson Machado Santos

Data Engineer at Xepelin. Experience in building data ecosystems which gather data and turn them available to business users.