-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflightdeals.py
155 lines (134 loc) · 4.8 KB
/
flightdeals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import pandas as pd
import time
import datetime
browser = webdriver.Chrome(executable_path='chromedriver')
# Ticket type path
return_ticket = "//label[@id='flight-type-roundtrip-label-hp-flight']"
one_way_ticket = "//label[@id='flight-type-one-way-label-hp-flight']"
multi_ticket = "//label[@id='flight-type-multi-dest-label-hp-flight']"
# Choose ticket type
def ticket_chooser(ticket):
try:
ticket_type = browser.find_element_by_xpath(ticket)
ticket_type.click()
except Exception as e:
pass
# Choose departure country
def departure_city_chooser(departure_country):
fly_from = browser.find_element_by_xpath("//input[@id='flight-origin-hp-flight']")
time.sleep(1)
fly_from.clear()
time.sleep(1.5)
fly_from.send_keys(' ' + departure_country)
time.sleep(1.5)
first_item = browser.find_element_by_xpath("//a[@id='aria-option-0']")
time.sleep(1.5)
first_item.click()
# Choose arrival arrival country
def arrival_city_chooser(arrival_country):
fly_to = browser.find_element_by_xpath("//input[@id='flight-destination-hp-flight']")
time.sleep(1)
fly_to.clear()
time.sleep(1.5)
fly_to.send_keys(' ' + arrival_country)
time.sleep(1.5)
first_item = browser.find_element_by_xpath("//a[@id='aria-option-0']")
time.sleep(1.5)
first_item.click()
# Choose departure date
def departure_date_chooser(month, day, year):
dep_date_button = browser.find_element_by_xpath("//input[@id='flight-departing-hp-flight']")
dep_date_button.clear()
dep_date_button.send_keys(month + '/' + day + '/' + year)
# Choose return dates
def return_date_chooser(month, day, year):
return_date_button = browser.find_element_by_xpath("//input[@id='flight-returning-hp-flight']")
for i in range(11):
return_date_button.send_keys(Keys.BACKSPACE)
return_date_button.send_keys(month + '/' + day + '/' + year)
# Search!
def search():
search = browser.find_element_by_xpath("//button[@class='btn-primary btn-action gcw-submit']")
search.click()
time.sleep(10)
# Create data frame
df = pd.DataFrame()
def compile_data():
global df
global dep_times_list
global arr_times_list
global airlines_list
global price_list
global durations_list
global stops_list
global layovers_list
#departure times
dep_times = browser.find_elements_by_xpath("//span[@data-test-id='departure-time']")
dep_times_list = [value.text for value in dep_times]
#arrival times
arr_times = browser.find_elements_by_xpath("//span[@data-test-id='arrival-time']")
arr_times_list = [value.text for value in arr_times]
#airline name
airlines = browser.find_elements_by_xpath("//span[@data-test-id='airline-name']")
airlines_list = [value.text for value in airlines]
#prices
prices = browser.find_elements_by_xpath("//span[@data-test-id='listing-price-dollars']")
price_list = [value.text.split('$')[1] for value in prices]
#durations
durations = browser.find_elements_by_xpath("//span[@data-test-id='duration']")
durations_list = [value.text for value in durations]
#stops
stops = browser.find_elements_by_xpath("//span[@class='number-stops']")
stops_list = [value.text for value in stops]
#layovers
layovers = browser.find_elements_by_xpath("//span[@data-test-id='layover-airport-stops']")
layovers_list = [value.text for value in layovers]
for i in range(len(dep_times_list)):
try:
df.loc[i, 'departure_time'] = dep_times_list[i]
except Exception as e:
pass
try:
df.loc[i, 'arrival_time'] = arr_times_list[i]
except Exception as e:
pass
try:
df.loc[i, 'airline'] = airlines_list[i]
except Exception as e:
pass
try:
df.loc[i, 'duration'] = durations_list[i]
except Exception as e:
pass
try:
df.loc[i, 'stops'] = stops_list[i]
except Exception as e:
pass
try:
df.loc[i, 'layovers'] = layovers_list[i]
except Exception as e:
pass
try:
df.loc[i, 'prices'] = price_list[i]
except Exception as e:
pass
# Run code
for i in range(24):
link = 'https://www.expedia.com/'
browser.get(link)
time.sleep(5)
#choose flights only
flights_only = browser.find_element_by_xpath("//button[@id='tab-flight-tab-hp']")
flights_only.click()
ticket_chooser(return_ticket)
departure_city_chooser('Bilbao')
arrival_city_chooser('London')
departure_date_chooser('10', '03', '2019')
return_date_chooser('20', '03', '2019')
search()
compile_data()
print(df)
time.sleep(3600)