Skip to content

Instantly share code, notes, and snippets.

@mmowbray
Last active February 14, 2024 20:34
Show Gist options
  • Save mmowbray/3d9f2bc2c810c8f4210dc8b0a609892a to your computer and use it in GitHub Desktop.
Save mmowbray/3d9f2bc2c810c8f4210dc8b0a609892a to your computer and use it in GitHub Desktop.
Tesla price watcher 2023
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)
['my@email.com'], # List of recipient email addresses
fail_silently=False, # Set to True to suppress exceptions if sending fails
)
finally:
driver.quit()
return car_prices
@mlin26
Copy link

mlin26 commented Sep 6, 2023

Hi how can we get the item link for this price?

@mmowbray
Copy link
Author

mmowbray commented Sep 6, 2023

It would be some expression like:

carsection.getelement(By.Id, “link_button”).href

this is just pseudo code, you can use ChatGPT you get the exact expression if you give it the HTML of a car section :)

@natarajanj
Copy link

sorry what is the expected result when i run this code ? will it email if i update mmowbray@mentlegen.com to my email address ? I ran this is jupyter notebooks but got no results , first time trying something like this

@bjv
Copy link

bjv commented Sep 14, 2023

sorry what is the expected result when i run this code ? will it email if i update mail edited to my email address ? I ran this is jupyter notebooks but got no results , first time trying something like this

in a very broad way: yes. but if you do not call the watch_tesla function, the script will not do anything. furthermore, you will need to set some more configurations and maybe code fixes, which are not explicitly documented. this script is not very beginner-friendly.

@bjv
Copy link

bjv commented Sep 14, 2023

@mmowbray Great job. I am running a modified version on the german website of tesla now. Added some features like uploading the scraping results to google sheets. currently cars come and go frequently here. maybe one can find some kind of pattern ;)

@mmowbray
Copy link
Author

@bjv Sehr gut!

Glad to see this is helping people out!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment