Last active
November 12, 2024 06:05
-
-
Save mmowbray/3d9f2bc2c810c8f4210dc8b0a609892a to your computer and use it in GitHub Desktop.
Tesla price watcher 2023
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from celery import shared_task | |
from selenium import webdriver | |
from selenium.webdriver.support.wait import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.common.by import By | |
from django.core.mail import send_mail | |
from django.conf import settings | |
TESLA_URL = 'https://www.tesla.com/en_CA/inventory/new/my?TRIM=LRAWD&PAINT=BLACK&ADL_OPTS=TOWING&arrangeby=plh&zip=h4r0j1&range=200' | |
def watch_tesla(): | |
# test using Chrome Selenium | |
options = webdriver.ChromeOptions() | |
options.add_argument('--no-sandbox') | |
options.add_argument('--headless') | |
options.add_argument('--ignore-certificate-errors') | |
options.add_argument('--disable-dev-shm-usage') | |
options.add_argument('--disable-extensions') | |
options.add_argument('--disable-gpu') | |
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36' | |
options.add_argument('user-agent={0}'.format(user_agent)) | |
driver = webdriver.Chrome(options=options) | |
driver.set_page_load_timeout(90) | |
# Load the URL and get the page source | |
driver.get(TESLA_URL) | |
car_prices = [] | |
try: | |
results_container = WebDriverWait(driver, 100).until( | |
EC.presence_of_element_located((By.CLASS_NAME, "results-container")) | |
) | |
car_sections = results_container.find_elements(By.CLASS_NAME, "result-header") | |
# Now you can iterate over these article elements and perform any actions you desire. | |
for car_section in car_sections: | |
car_price_str = car_section.find_element(By.CLASS_NAME, "result-purchase-price").get_attribute("innerHTML") | |
car_price = int(car_price_str.replace('$', '').replace(',', '')) | |
car_prices.append(car_price) | |
if car_price < 73290: | |
send_mail( | |
'Model Y for sale gucci price', # Subject of the email | |
f'There is a model Y for sale for {car_price_str}!', # Message body | |
settings.EMAIL_HOST_USER, # From email address (sender) | |
['[email protected]'], # List of recipient email addresses | |
fail_silently=False, # Set to True to suppress exceptions if sending fails | |
) | |
finally: | |
driver.quit() | |
return car_prices |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@bjv Sehr gut!
Glad to see this is helping people out!!!