-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsite_visit.py
67 lines (47 loc) · 2.07 KB
/
site_visit.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
from prettytable import PrettyTable
from splinter import Browser
import time
from settings import *
SAVE_SCREENSHOT_AT_LOCATION = BASE_PATH + '1.png'
SAVE_TEXT_AT_LOCATION = BASE_PATH + 't.txt'
def search_flight():
try:
browser = Browser('chrome', headless=True)
browser.visit(SITE_NAME)
browser.find_by_xpath(CLICK_FROM).click()
browser.find_by_xpath(FROM).fill(FROM_PLACE)
browser.find_by_xpath(CLICK_TO).click()
browser.find_by_xpath(TO).fill(TO_PLACE)
browser.find_by_xpath(CLICK_ORIGIN_DEPARTURE).click()
browser.find_by_xpath(ENTER_ORIGIN_DEPARTURE_DATE).fill(DEPARTURE_FROM_US)
browser.find_by_xpath(CLICK_DESTINATION_DEPARTURE).click()
browser.find_by_xpath(ENTER_DESTINATION_DEPARTURE_DATE).fill(DEPARTURE_FROM_INDIA)
browser.find_by_xpath(CLICK_FLEXIBLE_DATES).click()
time.sleep(5)
browser.execute_script("window.scrollTo(0, 165)")
browser.driver.save_screenshot(SAVE_SCREENSHOT_AT_LOCATION)
time.sleep(10)
diction = {}
for i in range(4, 13):
diction[i] = ' '.join(browser.find_by_xpath(FLIGHT_DETAILS + str(i) + ']').text.split('\n\n')).split('\n')
table_1 = PrettyTable(['Price', 'Trip-Type', 'Timings', 'Airlines', 'Hours', 'Stops', 'Layover'])
table_2 = PrettyTable(['Price', 'Trip-Type', 'Timings/Airlines', 'Hours', 'Stops', 'Layover'])
for key, value in diction.items():
if len(diction.get(key)) == 7:
table_1.add_row(diction.get(key))
elif len(diction.get(key)) == 6:
table_2.add_row(diction.get(key))
print(table_1)
print('\n Addition flights... \n')
print(table_2)
data_1 = table_1.get_string()
data_2 = table_2.get_string()
with open('t.txt', 'w') as f:
f.write(data_1)
f.write('\n Additional flight details... \n')
f.write(data_2)
browser.quit()
except AttributeError:
print('Element has no attribute "text"')
else:
browser.quit()