Many people looking to take a plane out of state will look to keep track of the latest price changes. As a programmer you may like to give yourself a challenge and build a tool to monitor those prices for you. Many websites can easily be scraped using a programing language called python. This ChatGPT prompt will help you get started
Can you write a python script that can scrape a airline website every 30 minutes for the newest prices on a flight from cleveland ohio to las vegas.
Sure, here’s an example Python script using the BeautifulSoup and requests modules to scrape the United Airlines website for the latest prices on a flight from Cleveland, Ohio to Las Vegas every 30 minutes. Note that this script is just an example and may need to be adjusted to work with the specific airline website you want to scrape.
import requests
from bs4 import BeautifulSoup
import time
def scrape_flight_prices():
# Send a GET request to the United Airlines website
url = 'https://www.united.com/en-us/flights-from-cleveland-to-las-vegas'
response = requests.get(url)
# Parse the HTML response with BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser')
# Find the element containing the flight price
price_elem = soup.find('span', class_='fare-amount')
# Extract the price from the element's text
price = price_elem.text.strip()
# Print the price to the console
print(f"Latest price: {price}")
while True:
# Scrape the flight prices every 30 minutes
scrape_flight_prices()
time.sleep(1800) # Sleep for 30 minutes
In this script, the scrape_flight_prices()
function sends a GET request to the United Airlines website for the Cleveland to Las Vegas flight, parses the HTML response with BeautifulSoup, finds the element containing the flight price, extracts the price from the element’s text, and prints the price to the console.
The main part of the script uses an infinite loop to call the scrape_flight_prices()
function every 30 minutes using the time.sleep()
function to pause the script for that time period.
What about for flights leaving on july 1st and returning july 4th
Here’s an updated version of the Python script that scrapes the United Airlines website for the latest prices on a round-trip flight from Cleveland, Ohio to Las Vegas leaving on July 1st and returning on July 4th.
import requests
from bs4 import BeautifulSoup
import time
def scrape_flight_prices():
# Send a GET request to the United Airlines website
url = 'https://www.united.com/en-us/flights-from-cleveland-to-las-vegas'
response = requests.get(url)
# Parse the HTML response with BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser')
# Find the elements containing the outbound and return dates
outbound_elem = soup.find('input', {'name': 'departureDate_1'})
return_elem = soup.find('input', {'name': 'departureDate_2'})
# Set the outbound and return dates to July 1st and July 4th
outbound_elem['value'] = '2022-07-01'
return_elem['value'] = '2022-07-04'
# Submit the form to update the search results
form = soup.find('form', {'id': 'flightBooking'})
response = requests.post(form['action'], data=form.form_data())
# Parse the updated HTML response with BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser')
# Find the element containing the flight price
price_elem = soup.find('span', class_='fare-amount')
# Extract the price from the element's text
price = price_elem.text.strip()
# Print the price to the console
print(f"Latest price for July 1-4: {price}")
while True:
# Scrape the flight prices every 30 minutes
scrape_flight_prices()
time.sleep(1800) # Sleep for 30 minutes
In this script, the scrape_flight_prices()
function sends a GET request to the United Airlines website for the Cleveland to Las Vegas flight, parses the HTML response with BeautifulSoup, finds the elements containing the outbound and return dates, sets the outbound and return dates to July 1st and July 4th, submits the form to update the search results, parses the updated HTML response with BeautifulSoup, finds the element containing the flight price, extracts the price from the element’s text, and prints the price to the console.
The main part of the script uses an infinite loop to call the scrape_flight_prices()
function every 30 minutes using the time.sleep()
function to pause the script for that time period.
This tool although scrapes only one airline’s website, the chatgpt prompt could easily be modified to search multiple websites at once.