11/8/2023 0 Comments Expedia flight price trackerPut all those lists side by side as columns in our DataFrame.Store them in the related variable we created as a list.Find all the elements for an attribute (for example, all departure times).Create variables for all the flight attributes (highlighted in the previous picture) to be stored in lists.First, create a Pandas DataFrame to hold our data.We will use this sequence to compile our data: The resulting webpage is as follows (with the fields I am interested in marked): Here it is better to use a long delay of 15 seconds or so to make sure all results are loaded before we proceed to the next steps. Getting the Resultsĭefine the function that will click the search button. I put it in a for loop to click backspace 11 times to delete all the characters for the date in the field. The way I worked around this is by using Keys.BACKSPACE which simply tells Python to click backspace (to delete whatever is written in the date field). Return_date_nd_keys(month + '/' + day + '/' + year)įor the return date, clearing whatever was written wasn't working for some reason (probably due to the page having this as autofill not allowing me to override it with. Return date: def return_date_chooser(month, day, year): Fill the element with the month, day, and year entered in the function as arguments + the slashes for date format.Find the element on the web page like before.def arrival_country_chooser(arrival_country):įirst_item = Choosing the Departure and Return Datesĭeparture date: def dep_date_chooser(month, day, year):ĭep_date_button = + '/' + day + '/' + year) Let's do the same for the arrival country. Without time.sleep , sometimes our script acts faster than the page loads and thus tries to access elements that didn't load yet causing our code to break. Note that I am using time.sleep between steps to give a chance to the page's elements to update/load between steps. Choose the first choice that appears from the drop-down menu (also using its tag and id which can be found by right click + inspect on the element when the drop-down menu appears).Type in the country I want (that will be passed into the function) using.Clear any value written in the country field.Find the element using its tag and attributes. ![]() Choose Departure and Arrival Countriesīelow I define a function to choose the departure country. The above sequence is the same sequence I will use for the rest of the code (look for tags and ids or other attributes and define a function to make the choice on the web page). Ticket_type = browser.find_element_by_xpath(ticket) Return_ticket = I define a function to choose a ticket type: def ticket_chooser(ticket): I click right click + inspect on the ticket type (roundtrip, one way, etc.) to see the tags related to it.Īs we can see below it has a 'label' tag with 'id = flight-type-roundtrip-label-hp-flight'.Īccordingly, I will use those to store the tags and ids for the three different ticket types as follows: #Setting ticket types paths Next, I will quickly go to Expedia to check the interface and the options available to choose from. This will open an empty browser telling you that this browser is being controlled by automated test software like so: Let's Get Coding Connect to the Web Browser browser = webdriver.Chrome(executable_path='/chromedriver') Note: I will not go too deeply into web scraping using selenium, but if you want a more detailed tutorial for scraping in general check my previous tutorials for scraping using Selenium and web scraping in general Part 1 and Part 2. We need those for connecting to our email and sending our message: import smtplibįrom import MIMEMultipart Time and date-time (for using delays and returning current time we will see why later): import time Pandas (we will mainly just used Pandas for structuring our data): import pandas as pd ![]() Selenium (for accessing websites and automation testing): from selenium import webdriverįrom import Byįrom import Keys Send the best rate for the current hour. ![]() Compile all available flights in a structured format (for those who love to do some exploratory data analysis!).Select the arrival country (if round trip).Choose the ticket type based on our preference (round trip, one way, etc.).Connect Python to our web browser and access the website (Expedia in our example here).In this tutorial, I will show you how to use Python to automatically surf a website like Expedia on an hourly basis looking for flights and sending you the best flight rate for a particular route you want every hour straight to your email.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |