Skip to content

Instantly share code, notes, and snippets.

@moonblade
Created September 22, 2018 11:40
Show Gist options
  • Save moonblade/86c01c3278aaa82bf4fda85ecc7a47ca to your computer and use it in GitHub Desktop.
Save moonblade/86c01c3278aaa82bf4fda85ecc7a47ca to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import requests
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 bs4 import BeautifulSoup
cacheNumbers = {
'times' : '6821',
'airline': '5532',
'duration': '6838',
'price' : '8443',
}
def getLink(date, fromPlace, toPlace):
return "https://www.google.com/flights/#flt="+fromPlace+"."+toPlace+".2018-10-"+date+";c:INR;e:1;sd:1;t:f;tt:o"
dates = ['01','02','03','04','05']
browser = webdriver.Firefox()
wait = WebDriverWait(browser, 10)
for date in dates:
string = ""
browser.get(getLink(date, "HYD", "CCJ"))
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".gws-flights-results__select-header.gws-flights__flex-filler")))
html = browser.page_source
string+="<dateTag date="+date+">"
string+=html.encode('ascii', 'ignore').decode('ascii')
string+="</dateTag>"
soup = BeautifulSoup(string, "html.parser")
for date in soup.find_all("datetag"):
dateStr = date['date']
for element in date.findChildren('div', {'class': 'gws-flights-results__collapsed-itinerary gws-flights-results__itinerary'}):
data = element.findChildren('span', {"jstcache": cacheNumbers['times']})
fromTime = data[0].text
toTime = data[1].text
data = element.findChildren('span', {"jstcache": cacheNumbers['airline']})
airline = data[0].text
data = element.findChildren('div', {"jstcache": cacheNumbers['duration']})
duration = data[0].text
data = element.findChildren('jsl', {"jstcache": cacheNumbers['price']})
price = int(data[0].text.encode('ascii', 'ignore').decode('ascii').replace(',',''))
print(dateStr, fromTime, toTime, airline, duration, price)
browser.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment