Home > front end >  Is it possible to manipulate lists of results from several functions into one new list?
Is it possible to manipulate lists of results from several functions into one new list?

Time:12-19

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[filter_enum_generation]=gen-ii-2015&search[filter_float_year:to]=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[sumar][rubricaId]=5&search[sumar][subrubricaId]=30&search[fields][0][fields][0][value]=Volvo&search[fields][0][fields][0][fields][1][value][]=XC90&search[fields][0][fields][2][value][min]=&search[fields][0][fields][2][value][max]=&search[fields][0][fields][3][value][min]=2017&search[fields][0][fields][3][value][max]=&search[fields][0][fields][5][value][min]=&search[fields][0][fields][5][value][max]=&search[fields][0][fields][7][value][min]=&search[fields][0][fields][7][value][max]=&search[cautareId]=&search[query]=&search[lat]=&search[lng]=&search[sortf]=valabilitate.sort&search[sorts]=-1&search[page]=&search[owner]=")
    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

CodePudding user response:

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:

average_price = sum(all_cars) / len(all_cars)
  • Related