1

I want to make a new list including the results from all three def functions and I do not know how. I am expecting to add all three results from the sites into a new list and then find the lowest price from the list, the highest price, maybe average price(but these i think I can do them by myself).

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 30)

 def bestauto(driver):
    driver.get("https://www.bestauto.ro/auto/?q=volvo+xc90+2020&pag=1&carregistrationdate=2020-2021")
    wait.until(EC.element_to_be_clickable((By.ID, 'cookieAccept'))).click()
    block = driver.find_elements(By.XPATH, ".//div[@class='listing-data']")

    result_bestauto = []
    for i in block:
        prices = i.find_element(By.XPATH, ".//strong[@class='price maincolor']").text
        price = prices.strip(' EUR').replace(' ', '.')
        result_bestauto.append(price)
    return result_bestauto

def autovit(driver):
    driver.execute_script("window.open('');")
    driver.switch_to.window(driver.window_handles[1])
    driver.get("https://www.autovit.ro/autoturisme/volvo/xc-90/seg-suv/de-la-2020?search%5Bfilter_enum_generation%5D=gen-ii-2015&search%5Bfilter_float_year%3Ato%5D=2021")
    wait.until(EC.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler'))).click()
    wait.until(EC.element_to_be_clickable((By.XPATH, ".//div[@class='ooa-z4wqde eg0t0xb1']"))).click()
    cars = driver.find_elements(By.XPATH, ".//article[@class='ooa-1txt27o e1b25f6f0']")

    result_autovit = []
    for y in cars:
            car_cost = y.find_element(By.XPATH, ".//span[@class='ooa-1bmnxg7 e1b25f6f11']").text
            cost = car_cost.strip("EUR ").replace(' ', '.')
            au = float(cost)
            au_dec = "{:.3f}".format(au)
            result_autovit.append(au_dec)

    if len(cars) < 6:
        promoted_car = driver.find_element(By.XPATH, ".//article[@class='ooa-1wl9plw e1b25f6f0']")
        pretulMasinii = promoted_car.find_element(By.XPATH, ".//span[@class='ooa-1bmnxg7 e1b25f6f11']").text
        pret = pretulMasinii.strip("EUR ").replace(' ', '.')
        a = float(pret)
        result_autovit.append(a)

    return result_autovit #= class type

def anuntul_ro(driver):
    driver.execute_script("window.open('');")
    driver.switch_to.window(driver.window_handles[2])
    driver.get("https://www.anuntul.ro/anunturi-auto-moto/autoturisme/?search%5Bsumar%5D%5BrubricaId%5D=5&search%5Bsumar%5D%5BsubrubricaId%5D=30&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B0%5D%5Bvalue%5D=Volvo&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B0%5D%5Bfields%5D%5B1%5D%5Bvalue%5D%5B%5D=XC90&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B2%5D%5Bvalue%5D%5Bmin%5D=&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B2%5D%5Bvalue%5D%5Bmax%5D=&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B3%5D%5Bvalue%5D%5Bmin%5D=2017&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B3%5D%5Bvalue%5D%5Bmax%5D=&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B5%5D%5Bvalue%5D%5Bmin%5D=&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B5%5D%5Bvalue%5D%5Bmax%5D=&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B7%5D%5Bvalue%5D%5Bmin%5D=&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B7%5D%5Bvalue%5D%5Bmax%5D=&search%5BcautareId%5D=&search%5Bquery%5D=&search%5Blat%5D=&search%5Blng%5D=&search%5Bsortf%5D=valabilitate.sort&search%5Bsorts%5D=-1&search%5Bpage%5D=&search%5Bowner%5D=")
    time.sleep(3)
    wait.until(EC.element_to_be_clickable((By.ID, 'acordCookies'))).click()
    masini = driver.find_elements(By.XPATH, ".//div[@class='clearfix line-btw anunt-row i-cb i-pr anunt-w  ']")
    result_anuntul_ro = []
    for x in masini:
        pret_masina = x.find_element(By.XPATH, ".//div[@class='float-right price-list i-fr']").text
        valuare = pret_masina.strip(" €").replace(' ', '.')
        an = float(valuare)
        an_dec = "{:.3f}".format(an)
        result_anuntul_ro.append(an_dec)
    return result_anuntul_ro

if __name__ == "__main__":
    result1 = bestauto(driver)
    result2 = autovit(driver)
    result3 = anuntul_ro(driver)
    all_cars = result1 + result2 + result3
E_net4
  • 27,810
  • 13
  • 101
  • 139

1 Answers1

0

You can concatenate the 3 lists into 1 list as following:

import itertools
all_cars = list(itertools.chain(result1, result2, result3))

now you will be able to find minimal and maximal values as following:

minimal_price = min(all_cars)
maximal_price = max(all_cars)

As about average value - there are several ways to get it, I'd prefer this:

L = [float(n) for n in all_cars if n]
average_price = sum(L)/len(L) if L else '-'
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • It doesn't combine the three results into one list unfortunetly. Thank you for the other operations! –  Dec 18 '22 at 18:26
  • OK, sorry. Previous way should work for 2 lists. The updated code should work for any amount of lists to be concatenated. – Prophet Dec 18 '22 at 18:40
  • Could you please explain what is itertools and when is it best to use it? I've never heard of it before. –  Dec 18 '22 at 18:42
  • Try googling about it. I hope you will find clear and precise description about it, better than I can give. But if you could not - let me know, I will try to explain. – Prophet Dec 18 '22 at 18:45
  • Implemented the averege operation but the following error pops up: TypeError: unsupported operand type(s) for +: 'int' and 'str'. I also want to keep the 3 decimals. The list now is made of string elements and the len gives int type. –  Dec 18 '22 at 19:22
  • From the error I understand that some values in the lists are of `int` type and the others are strings? Corect? – Prophet Dec 18 '22 at 19:28
  • len(all_cars) is int type and type(all_cars) is string –  Dec 18 '22 at 19:29
  • This is clear. But all the values inside all the 3 lists are ints? – Prophet Dec 18 '22 at 20:32
  • No, they are strings. –  Dec 19 '22 at 10:07
  • OK, but I do not understand. You mentioned that error is `TypeError: unsupported operand type(s) for +: 'int' and 'str'` but since all the values inside the lists are string - what `int` are we talking about that causes that types mismatch? – Prophet Dec 19 '22 at 11:17
  • I suppose the len(all_cars) because it's an integer. –  Dec 19 '22 at 12:40
  • Have you tried yourself with my code the average operation if it works? –  Dec 19 '22 at 13:14
  • Actually I didn't debug that yesterday, but did it today. The updated answer works. – Prophet Dec 19 '22 at 13:25
  • I don't understand the if statement... if n then what? Also if L then what? Shouldn't there be a condition for if? –  Dec 19 '22 at 14:13
  • The solution is from here https://stackoverflow.com/a/21230155/3485434 – Prophet Dec 19 '22 at 14:21
  • You are more than welcome! – Prophet Dec 19 '22 at 14:34