I want to make a program that takes the table data cell elements of tables from a wholesale website (the tables show stock of disposable vapes), and then will display which items are low on stock on a Tkinter GUI. So far I am using selenium to go to different URLs of various vapes and print out the stock of certain flavors of those vapes depending on if they are below a certain number. I’ve added a Tkinter GUI which asks what stock you would like to check and launches selenium when you press on the ‘Vapes’ button. Would it be possible to display the HTML data on the Tkinter GUI itself, instead of just printing it to Terminal? Here is my code so far, I plan on adding more vapes and other items but only did two here so it doesn’t look as messy:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys from tkinter import * import time PATH = "C:Program Files (x86)chromedriver.exe" driver = webdriver.Chrome(PATH) def search_result(): # fume infinity driver.get("https://safagoods.com/vape-shop/disposable-vape-devices/fume-infinity-disposable-device") wait = WebDriverWait(driver, 15) table = wait.until( EC.visibility_of_element_located((By.CSS_SELECTOR, ".table.table-bordered tr[style='background: #eeeeee;']"))) data = [] rows = driver.find_elements_by_css_selector(".table.table-bordered tr[style='background: #eeeeee;']") for row in rows: qty = row.find_element_by_xpath("./td[1]").text stock = row.find_element_by_xpath("./td[2]").text name = row.find_element_by_xpath("./td[3]").text if int(stock) <= 50: data.append([stock, name]) print("Number left/Flavor/Fume Infinity Disposables 5PC") print(*data, sep='n') # Fume Extra driver.get("https://safagoods.com/vape-shop/disposable-vape-devices/fume-extra-10pc") wait = WebDriverWait(driver, 15) table = wait.until( EC.visibility_of_element_located((By.CSS_SELECTOR, ".table.table-bordered tr[style='background: #eeeeee;']"))) data = [] rows = driver.find_elements_by_css_selector(".table.table-bordered tr[style='background: #eeeeee;']") for row in rows: qty = row.find_element_by_xpath("./td[1]").text stock = row.find_element_by_xpath("./td[2]").text name = row.find_element_by_xpath("./td[3]").text if int(stock) <= 50: data.append([stock, name]) print("Number left/Flavor/Fume Extra Disposables 10PC") print(*data, sep='n') driver.close() #tkinter gui window and displayed options window = Tk() window.geometry("450x200") search = Label(window,text= "Which stock would you like to check?",font='times 15') search.place(x=20,y=20) #buttons b1 = Button(window,text="vapes",command=search_result,width = 12, bg= 'gray') b1.place(x=150,y=50) #b2 = Button(window,text="vape juices",command=search_result,width = 12, bg= 'gray') #b2.place(x=150,y=90) #b2 = Button(window,text="Juices",command=vape_juices,width = 12, bg= 'gray') #b2.place(x=300,y=50) window.mainloop()
Sorry if this question is confusing I’m fairly new to programming. Thanks guys.
Advertisement
Answer
You could try using a Treeview.
I’ve added one in the code below, it’s a bit rough though.:)
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys from tkinter import * from tkinter import ttk import time PATH = "chromedriver.exe" driver = webdriver.Chrome(PATH) def search_result(): # fume infinity driver.get("https://safagoods.com/vape-shop/disposable-vape-devices/fume-infinity-disposable-device") wait = WebDriverWait(driver, 15) table = wait.until( EC.visibility_of_element_located((By.CSS_SELECTOR, ".table.table-bordered tr[style='background: #eeeeee;']"))) data = [] rows = driver.find_elements_by_css_selector(".table.table-bordered tr[style='background: #eeeeee;']") for row in rows: qty = row.find_element_by_xpath("./td[1]").text stock = row.find_element_by_xpath("./td[2]").text name = row.find_element_by_xpath("./td[3]").text if int(stock) <= 50: data.append([stock, name]) #populate treeview for idx, vals in enumerate(data): print(idx) tree.insert(parent='', index=idx, values=vals) print("Number left/Flavor/Fume Infinity Disposables 5PC") print(*data, sep='n') # Fume Extra driver.get("https://safagoods.com/vape-shop/disposable-vape-devices/fume-extra-10pc") wait = WebDriverWait(driver, 15) table = wait.until( EC.visibility_of_element_located((By.CSS_SELECTOR, ".table.table-bordered tr[style='background: #eeeeee;']"))) data = [] rows = driver.find_elements_by_css_selector(".table.table-bordered tr[style='background: #eeeeee;']") for row in rows: qty = row.find_element_by_xpath("./td[1]").text stock = row.find_element_by_xpath("./td[2]").text name = row.find_element_by_xpath("./td[3]").text if int(stock) <= 50: data.append([stock, name]) print("Number left/Flavor/Fume Extra Disposables 10PC") print(*data, sep='n') driver.close() #tkinter gui window and displayed options window = Tk() window.geometry("450x500") search = Label(window,text= "Which stock would you like to check?",font='times 15') search.place(x=20,y=20) #buttons b1 = Button(window,text="vapes",command=search_result,width = 12, bg= 'gray') b1.place(x=150,y=50) # add treeview tree = ttk.Treeview(window, ) tree['columns'] = ['Quantity', 'Name'] tree.column('#0', width=0, stretch=NO) tree.column('Quantity', anchor=CENTER, width=100) tree.column('Name', anchor=CENTER, width=200) tree.heading('#0', text='', anchor=CENTER) tree.heading('Quantity', text='Quantity', anchor=CENTER) tree.heading('Name', text='Name', anchor=CENTER) tree.place(x=0, y=200) #b2 = Button(window,text="vape juices",command=search_result,width = 12, bg= 'gray') #b2.place(x=150,y=90) #b2 = Button(window,text="Juices",command=vape_juices,width = 12, bg= 'gray') #b2.place(x=300,y=50) window.mainloop()