Skip to content
Related Articles

Related Articles

Flight-price checker using Python and Selenium
  • Last Updated : 16 Jul, 2020

Python is a scripting language with many extended libraries and frameworks. It is used in various fields of computer science such as web development and data science. Also, Python can be used to automate some minor tasks which can be really helpful in the long run.

The Python script mentioned in this article will fetch the prices from paytm.com using selenium and if it is lesser or than equal to the amount(you are ready to spend) set by you, then a notification will be sent to the desired email-ids about the reduced price.

The email id which will be used to send the notifications should not use the personal password since it requires the two-factor authentication and the action will fail. Rather, a different password should be set which can be accessed by the script and many other different applications one might develop in the future.

Note: The password required for sending that email can be set through this link: Google App Passwords

Implementation

Importing all the required libraries



Selenium to perform automation of the website




from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.chrome.options import Options 
from selenium.common.exceptions import TimeoutException 
from selenium.webdriver.common.keys import Keys 

STMPLIB for sending notifications through emails




import smtplib

Extracting the price of flights between two selected dates




# Choose the two dates
# in this format 
x = "2020-03-10" 
y = "2020-03-16"
  
a = int(x[8:10])
b = int(y[8:10])
  
if a > b:
    m = a - b
    t = b
  
else:
    m = b - a
    t = a
print(t)
  
low_price = ""
data = {}
  
for i in range(t, t + m+1):
    url = 'https://paytm.com/flights/flightSearch/BBI-\
    Bhubaneshwar/DEL-Delhi/1/0/0/E/2020-03-'+str(i)
      
    # Locations can be changed on 
    # the above statement
    print(url)
      
    date = "2019-12-" + str(i)
      
    # enables the script to run properly without 
    # opening the chrome browser.
    chrome_options = Options()
    chrome_options.add_argument("--disable-gpu")
      
  
    chrome_options.add_argument("--headless")
      
    driver = webdriver.Chrome(executable_path = '/path/to/chromedriver'
                              options=chrome_options)
      
    driver.implicitly_wait(20)
    driver.get(url)
      
    g = driver.find_element_by_xpath("//div[@class='_2gMo']"
    price = g.text
      
    x = price[0]
    y = price[2:5]
    z = str(x)+str(y)
    p = int(z)
    print(p)
      
    prices=[]
    if p <= 2000:
        data[date] = p
          
for i in data:
    low_price += str(i) + ": Rs." + str(data[i]) + "\n"
      
print(low_price) 

Sending the notification if there are cheap flights available through an email using SMTP




if len(data) != 0:
      
    dp = 2000
    server = smtplib.SMTP('smtp.gmail.com',587)
    server.ehlo()
    server.starttls()
    server.ehlo()
      
    server.login('your_email_id','your_password')
    subject = "Flight price for BBI-DEL has fallen\
    below Rs. " + str(dp)
      
    body = "Hey Akash! \n The price of BBI-DEL on PayTm \
    has fallen down below Rs." + str(dp) + ".\n So,\
    hurry up & check: " + url_final+"\n\n\n The prices of\
    flight below Rs.2000 for the following days are\
    :\n\n" + low_price
      
    msg = f"Subject: {subject} \n\n {body}"
      
    server.sendmail(
        # email ids where you want to
        # send the notification
        'email_id_1',
        'email_id_2',
        msg
        )
      
    print("HEY,EMAIL HAS BEEN SENT SUCCESSFULLY.")
       
    server.quit()

Entire Code:




from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.chrome.options import Options 
from selenium.common.exceptions import TimeoutException 
from selenium.webdriver.common.keys import Keys 
import smtplib
  
  
# Choose the two dates
# in this format 
x = "2020-03-10" 
y = "2020-03-16"
  
a = int(x[8:10])
b = int(y[8:10])
  
if a > b:
    m = a - b
    t = b
  
else:
    m = b - a
    t = a
print(t)
  
low_price = ""
data = {}
  
for i in range(t, t + m+1):
    url = 'https://paytm.com/flights/flightSearch/BBI-\
    Bhubaneshwar/DEL-Delhi/1/0/0/E/2020-03-'+str(i)
      
    # Locations can be changed on 
    # the above statement
    print(url)
      
    date = "2019-12-" + str(i)
      
    # enables the script to run properly without 
    # opening the chrome browser.
    chrome_options = Options()
    chrome_options.add_argument("--disable-gpu")
      
  
    chrome_options.add_argument("--headless")
      
    driver = webdriver.Chrome(executable_path = '/path/to/chromedriver'
                              options=chrome_options)
      
    driver.implicitly_wait(20)
    driver.get(url)
      
    g = driver.find_element_by_xpath("//div[@class='_2gMo']"
    price = g.text
      
    x = price[0]
    y = price[2:5]
    z = str(x)+str(y)
    p = int(z)
    print(p)
      
    prices=[]
    if p <= 2000:
        data[date] = p
          
for i in data:
    low_price += str(i) + ": Rs." + str(data[i]) + "\n"
      
print(low_price) 
  
if len(data) != 0:
      
    dp = 2000
    server = smtplib.SMTP('smtp.gmail.com',587)
    server.ehlo()
    server.starttls()
    server.ehlo()
      
    server.login('your_email_id','your_password')
    subject = "Flight price for BBI-DEL has fallen\
    below Rs. " + str(dp)
      
    body = "Hey Akash! \n The price of BBI-DEL on PayTm \
    has fallen down below Rs." + str(dp) + ".\n So,\
    hurry up & check: " + url_final+"\n\n\n The prices of\
    flight below Rs.2000 for the following days are\
    :\n\n" + low_price
      
    msg = f"Subject: {subject} \n\n {body}"
      
    server.sendmail(
        # email ids where you want to
        # send the notification
        'email_id_1',
        'email_id_2',
        msg
        )
      
    print("HEY,EMAIL HAS BEEN SENT SUCCESSFULLY.")
       
    server.quit()

Output:

python-selenium

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :