Hello,
I tried to extract data from a website but it didn't work. Can you please tell me where is the error?
from selenium import webdriver # Import module
from selenium.webdriver.common.keys import Keys # For keyboard keys
from time import sleep
URL = 'https://www.comobila.com/covoiturage/trajet/rechercher'
#port=0, executable_path="/usr/bin/safaridriver", quiet=False
driver = webdriver.Safari(executable_path="/usr/bin/safaridriver")
driver.maximize_window()
driver.get(URL)
drivers = []
from_to = []
dates_times = []
prices = []
sleep(20)
drivers_info = driver.find_elements_by_xpath('//div[@class = "recent-ride-row-img"]')
trip_info = driver.find_elements_by_xpath('//div[@class = "recent-ride-row-info"]')
trip_price = driver.find_elements_by_xpath('//div[@class = "recent-ride-row-price"]')
for i in range(len(drivers_info)):
drivers.append(drivers_info[i].text.split('\n'))
from_to.append(trip_info[i].text.split('\n')[0].split(' '))
if trip_info[i].text.split('\n')[1].startswith('Date'):
dates_times.append(tripinfos[i].text.split('\n')[1].split(': ')[1].split(' - '))
else:
dates_times.append(tripinfos[i].text.split('\n')[2].split(': ')[1].split(' - '))
prices.append(tripprice[i].text.split('\n')[0])
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
sleep(20)
driver.find_element_by_xpath("/html/body/div[2]/div[1]/div[2]/div[2]/div[2]/div/div[2]/div/div[4]/ul/li[13]/a").click()
out = open('COVTrips.csv','w')
out.write('driver_name;driver_sex;driver_age;driver_is_smoking;from;to;date;time;price\n')
for i in range(len(drivers)):
out.write(drivers[i][0]+';'+drivers[i][1].split(' | ')[0]+';'+drivers[i][1].split(' | ')[1]+';'+drivers[i][2].split(' : ')[1]+';'+from_to[i][0]+';'+from_to[i][1]+';'+dates_times[i][0]+';'+dates_times[i][1]+';'+prices[i]+'\n');
driver.close()