Uncategorized

Code for Selenium Python Price Tracker

import time
from datetime import datetime
from csv import writer
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
#REQUIRED FOR HEADLESS PART 1
from selenium.webdriver.chrome.options import Options
#REQUIRED FOR HEADLESS

from selenium.webdriver.chrome.service import Service

while(True):
    try:
        print(time.time())
        s = Service('/usr/local/bin/chromedriver')
        #driver = webdriver.Chrome(service=s)

        #Set some selenium chrome options - Part 2
        chromeOptions = Options()
        chromeOptions.headless = True

        #This is where your chromedriver lives. You download this from google. It needs to be stored locally.
        #PATH = "/usr/local/bin/chromedriver"

        # Initialize Driver Headless by uncommenting appropriate driver below #part 3
        driver = webdriver.Chrome(service=s, options=chromeOptions)
        # driver = webdriver.Chrome(PATH)
        list_data = []

        def initialize_browser():
            driver.get("INSERT YOUR URL")
            time.sleep(1)
            print("starting_Driver")
            content = driver.find_element(By.CLASS_NAME, "price")
            price = content.text
            content = driver.find_element(By.CLASS_NAME, "post-title")
            pName = content.text
            this_moment = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
            list_data = [price, pName, this_moment]
            return list_data

        list_data = initialize_browser()

        with open('CSVFILE.csv', 'a', newline='') as f_object:
            # Pass the CSV  file object to the writer() function
            writer_object = writer(f_object)
            # Result - a writer object
            # Pass the data in the list as an argument into the writerow() function
            writer_object.writerow(list_data)
            # Close the file object
            f_object.close()

        driver.quit()
        time.sleep(15)
    except:
        print("problem with script")