是否有可能将几个函数的结果列表操作到一个新列表中?



我想做一个新的列表,包括从所有三个def函数的结果,我不知道如何。我希望将所有三个结果从网站添加到一个新的列表中,然后从列表中找到最低价格,最高价格,也许平均价格(但这些我认为我可以自己做)。

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:webdriverschromedriver.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

您可以将3个列表连接成一个列表,如下所示:

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

现在你将能够找到最小值和最大值如下:

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

关于平均值-有几种方法可以获得它,我更喜欢这样:

L = [float(n) for n in all_cars if n]
average_price = sum(L)/len(L) if L else '-'