Using Selenium Scraper to Find the Cheapest Flights!
The goal of this project is to build a web scraper that performs searches on flight prices with flexible dates (up to 3 days before and after the dates you select first) for a particular destination. The program saves an excel with the results and sends an alert email with the quick stats with the best deals.
While most of us would agree that traveling is a great way to experience new cultures and broaden our perspectives, the process of searching for the best price for our buck is rather a tedious procedure. I eventually chose to scrape HotWire because of the lack of Recaptcha. The idea of the structure of the web scraper is more or less like this:
- a function will start the bot, declaring the cities and dates we want to search
- this function gets the first search results, sorted by “best” flights
- another function will scrape the whole page, and output into an excel file
- it will repeat step 2 and 3 for the “cheap” sort types
- an email will be sent to you with a summary of the prices (cheapest and average)
As a start, I used Chrome driver as my web driver to open a blank Chrome tab and randint to make the bot sleep a random number of seconds between each search. To start getting prices and other information, I gathered all the XPaths to all the elements I wanted and used Python to select the cheapest results.
After inputting the travel destination in mind as well as the expected travel date, each desired result [airline, flight duration, layover(s) and layover duration, price] was scrapped and fetched using a series of for loops and printed into an excel file called flight data. The data are then re-read into Jupyter Notebook and the data was cleaned using regular expressions.
From the user front, the program prompts the users to declare the destination cities and the travel dates as inputs. From there, it will open the address in the HotWire string, which goes directly to the sort by “price” results page. The program will output the top 5 matrices with the cheapest flight prices and used the list of prices to calculate the average price of the 7-day travel range and highlight the minimum price as well. The result is then emailed to the user based on the HotWire searches. The code was tested using a Hotmail account, and the user can simply replace the information using their details if they were to utilize the program.
In conclusion, there are plenty of improvements can be made to the current program. I can code the program further so it will provide more travel options to the users [round trips, airline, flight class, etc], integrate the code with Twilio so the program will send out a text instead of email alert, figure a method to work around the ReCaptcha so the scraper can be applied to other search engines as well.