-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebdriver.py
164 lines (144 loc) · 7.87 KB
/
webdriver.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
156
157
158
159
160
161
162
163
164
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
import os
# Start a new instance of Chrome WebDriver
# Configure Chrome options for headless mode
def webdriverrun(departure, arrival, _date, num_passengers):
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless=old")
chrome_options.add_argument("--window-position=-10000,-10000")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-logging")
chrome_options.add_argument("--log-level=3")
chrome_options.add_argument("--silent")
chrome_options.add_argument("--disable-software-rasterizer")
chrome_options.add_argument("--disable-images") # Disable image loading
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.90 Safari/537.36")
chrome_options.add_argument("--window-size=1920x1080") # Set a specific window size in headless mode
chrome_options.add_argument("--disable-background-timer-throttling")
chrome_options.add_argument("--disable-backgrounding-occluded-windows")
chrome_options.add_argument("--disable-renderer-backgrounding")
chrome_options.add_argument("--no-first-run")
# Start a new instance of Chrome WebDriver with headless mode enabled
# chrome_options.add_argument("--log-path=C:/Users/erman/OneDrive/Desktop/chromedriver.log")
# chrome_options.add_argument("--verbose") # More detailed logging
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options) # Navigate to the webpage
driver.get("https://ebilet.tcddtasimacilik.gov.tr/view/eybis/tnmGenel/tcddWebContent.jsf")
driver.set_page_load_timeout(30)
trains_info = {}
try:
# Use WebDriverWait to wait until the page is fully loaded
WebDriverWait(driver, 20).until(
lambda driver: driver.execute_script("return document.readyState") == "complete")
# Find and fill the 'from' field
from_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div[2]/div/div[2]/ul/li[1]/div/form/div[1]/p[4]/input"))
)
from_field.send_keys(departure)
# Find and fill the 'to' field
to_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div[2]/div/div[2]/ul/li[1]/div/form/div[2]/p[4]/input"))
)
to_field.send_keys(arrival)
# Find and fill the 'depart date' field
depart_date_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div[2]/div/div[2]/ul/li[1]/div/form/div[1]/p[6]/span/input"))
)
depart_date_field.clear() # Clear existing content
depart_date_field.send_keys(_date)
driver.find_element(By.TAG_NAME, 'body').click()
# Find and fill the 'number of passenger' field
passenger_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div[2]/div/div[2]/ul/li[1]/div/form/div[3]/p[2]/span/input"))
)
passenger_field.click()
passenger_field.send_keys(Keys.DELETE)
passenger_field.send_keys(num_passengers)
# Find and click the 'find trains' button
find_trains_button = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div[2]/div/div[2]/ul/li[1]/div/form/div[3]/p[3]/button/span"))
)
find_trains_button.click()
# Wait until the growl message is visible
try:
growl_message = WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.CLASS_NAME, "ui-growl-message")))
# Capture the text of the growl message
message_text = growl_message.text
trains_info["Growl message"] = message_text
# print(train_info)
# time.sleep(5) # Hardcoded interval if necessary
except:
pass
# time.sleep(5) # Hardcoded interval if necessary
WebDriverWait(driver, 20).until(
lambda driver: driver.execute_script("return document.readyState") == "complete")
if "Growl message" not in trains_info:
# WebDriverWait(driver, 30).until(EC.url_changes(driver.current_url))
with open("page.html", "w", encoding="utf-8") as file:
file.write(driver.page_source)
# Now you can scrape the results page for the information you need
# Use BeautifulSoup or Selenium to extract the desired information from the page
# except TimeoutException as e:
# raise TimeoutException from e
finally:
# Close the WebDriver session
driver.quit()
if "Growl message" not in trains_info:
# Now you can use BeautifulSoup to parse the saved HTML page
with open("page.html", "r", encoding="utf-8") as file:
html_content = file.read()
os.remove("page.html")
# Parse the HTML content with BeautifulSoup
soup = BeautifulSoup(html_content, 'html.parser')
# Extracting the information
rows = soup.find_all('tr', {'data-ri': True})
for index, row in enumerate(rows):
departure_date = row.find_all('td')[0].get_text(strip=True).split('\n')[0]
# departure_time = row.find_all('td')[0].find('span').get_text(strip=True)
duration = row.find_all('td')[1].find_all('label')[1].get_text(strip=True)
arrival_date = row.find_all('td')[2].get_text(strip=True).split('\n')[0]
# arrival_time = row.find_all('td')[2].find('span').get_text(strip=True)
# train_type = row.find_all('td')[3].find_all('label')[1].get_text(strip=True)
# route = row.find_all('td')[3].find_all('label')[2].get_text(strip=True)
# availability_tag = soup.find('div', class_='ui-tooltip ui-widget ui-widget-content ui-shadow ui-corner-all').get_text(strip=True)
# price = soup.find('label', {'class': 'ui-outputlabel seferSorguTableBuyuk'}).get_text(strip=True)
wagon_type_options = row.find_all('option')
wagon_types = [opt.get_text(strip=True) for opt in wagon_type_options]
# Create a dictionary for the current train
train_info = {
'Departure Date': departure_date,
# 'Departure Time': departure_time,
'Duration': duration,
'Arrival Date': arrival_date,
# 'Arrival Time': arrival_time,
# 'Train Type': train_type,
# 'Route': route,
# 'Availability': availability_tag,
# 'Price': price,
'Wagon Types': wagon_types
}
# Add the current train info to the main dictionary
trains_info[f'Train {index + 1}'] = train_info
# print(trains_info)
# retries = 3
# if len(trains_info) == 0:
# for i in range(retries):
# trains_info = webdriverrun()
# if trains_info:
# break
# time.sleep(3)
return trains_info
# webdriverrun("Ankara", "Ankara", "29.09.2024", 4)