Selenium kann keine Klasse für Automarken und Preise finden und in einem lokalen HTML veröffentlichenPython

Python-Programme
Anonymous
 Selenium kann keine Klasse für Automarken und Preise finden und in einem lokalen HTML veröffentlichen

Post by Anonymous »

Ich habe eine Art Hausaufgaben von einem Praktikum erhalten und es klingt ungefähr so:
Machen Sie mir bitte ein Programm in Python, das in einem Docker-Container läuft und bei Start-up dienen wird Eine Webseite, auf der Folgendes passiert: < /p>

Um die Hauptmarken von Autos von autovit zu nehmen Die Auswahl einer Marke und nach der Auswahl, um die minimalen und maximalen Preise der Autos der jeweiligen Marke auf AutoVit anzuzeigen. Sie dürfen Hilfe von Chatgpt erhalten. Unten finden Sie den Code in Python für den Schaber: < /p>
from fastapi import FastAPI, Form
from fastapi.responses import HTMLResponse
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import time

app = FastAPI()

# Inițializează driver-ul Chrome cu Selenium
def get_selenium_driver():
options = webdriver.ChromeOptions()
options.headless = True # Rulează în mod headless (fără interfață grafică)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
return driver

# Funcție pentru a obține brandurile de pe autovit.ro
def get_car_brands():
url = "https://www.autovit.ro/"
driver = get_selenium_driver()
driver.get(url)

# Așteaptă să se încarce elementele (poți ajusta timpul după caz)
time.sleep(5)

# Obține sursa paginii
soup = BeautifulSoup(driver.page_source, 'html.parser')

# Încearcă să găsești brandurile folosind selectorul corect (actualizează-l cu unul corect)
brands = []
try:
for brand in soup.find_all('a', class_="e4zbkti0 ooa-17iu0re"): # Actualizează cu selectorul corect
brands.append(brand.text.strip())
except Exception as e:
print(f"Error while extracting brands: {e}")

driver.quit()
return brands

# Funcție pentru a obține prețurile minime și maxime ale unui brand
def get_price_range(brand):
url = f"https://www.autovit.ro/{brand}"
driver = get_selenium_driver()
driver.get(url)

# Așteaptă să se încarce elementele (ajustează timpul după caz)
time.sleep(5)

soup = BeautifulSoup(driver.page_source, 'html.parser')

# Găsește prețurile folosind selectorul corect
prices = []
try:
for price in soup.find_all('span', class_="ooa-ejqwvc"): # Actualizează cu selectorul corect
try:
price_value = int(price.text.replace("€", "").replace(",", "").strip())
prices.append(price_value)
except ValueError:
pass # În cazul în care prețul nu poate fi convertit într-un număr
except Exception as e:
print(f"Error while extracting prices: {e}")

driver.quit()
return min(prices), max(prices) if prices else (0, 0) # Asigură-te că returnezi o valoare validă

@app.get("/", response_class=HTMLResponse)
def read_root():
brands = get_car_brands()
brand_options = "".join([f"{brand}" for brand in brands])
html_content = f"""


Select a car brand


{brand_options}





"""
return html_content

@app.get("/price_range", response_class=HTMLResponse)
def price_range(brand: str = Form(...)):
min_price, max_price = get_price_range(brand)
html_content = f"""


Price Range for {brand}
Min Price: €{min_price}
Max Price: €{max_price}


"""
return html_content

< /code>
Das Problem ist der Container in Docker Works, ich kann den Browser öffnen und nach Localhost: 8000 und die HTML -Öffnung suchen, aber ich kann keine Automarke oder einen Preis finden. Ich fragte Chatgpt und kam auch zu dem Schluss, dass die Klassen falsch sind, aber ich kann nichts Besseres finden. Ich habe es auch mit XPath versucht, aber ohne Erfolg. Hast du irgendwelche Ideen?
Danke!

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post