|
| 1 | +import requests |
| 2 | +from bs4 import BeautifulSoup |
| 3 | +import tkinter as tk |
| 4 | +from tkinter import ttk |
| 5 | +from tkinter import font as tkFont |
| 6 | +from selenium import webdriver |
| 7 | +from selenium.webdriver.common.keys import Keys |
| 8 | +import time |
| 9 | + |
| 10 | +driver_path = input('Enter chrome driver path') |
| 11 | + |
| 12 | + |
| 13 | +# Function to scrape stock data from generated URL |
| 14 | +def scraper(): |
| 15 | + url = 'https://www.cointracker.io/price' |
| 16 | + driver = webdriver.Chrome(driver_path) |
| 17 | + driver.get(url) |
| 18 | + |
| 19 | + # Wait for results to load |
| 20 | + time.sleep(5) |
| 21 | + html = driver.page_source |
| 22 | + |
| 23 | + # Start scraping resultant html data |
| 24 | + soup = BeautifulSoup(html, 'html.parser') |
| 25 | + |
| 26 | + # Find the crypto price table to scrape |
| 27 | + results = soup.find("table", {"class": 'table mx-auto'}) |
| 28 | + rows = results.findChildren('tr') |
| 29 | + |
| 30 | + table_data = [] |
| 31 | + row_values = [] |
| 32 | + # Append individual cryptocurrency data into a list |
| 33 | + for row in rows: |
| 34 | + cells = row.findChildren(['th', 'td']) |
| 35 | + for cell in cells: |
| 36 | + value = cell.text.strip() |
| 37 | + value = " ".join(value.split()) |
| 38 | + row_values.append(value) |
| 39 | + table_data.append(row_values) |
| 40 | + row_values = [] |
| 41 | + |
| 42 | + # Formatting the cryptocurrency data stored in the list |
| 43 | + stocks_data = "" |
| 44 | + for stock in table_data: |
| 45 | + single_record = "" |
| 46 | + for cell in stock: |
| 47 | + format_cell = "{:<30}" |
| 48 | + single_record += format_cell.format(cell[:20]) |
| 49 | + single_record += "\n" |
| 50 | + stocks_data += single_record |
| 51 | + |
| 52 | + # Adding the formatted data into tkinter GUI |
| 53 | + query_label.config(state=tk.NORMAL) |
| 54 | + query_label.delete(1.0,"end") |
| 55 | + query_label.insert(1.0,stocks_data) |
| 56 | + query_label.config(state=tk.DISABLED) |
| 57 | + driver.close() |
| 58 | + |
| 59 | +# Creating tkinter window |
| 60 | +window = tk.Tk() |
| 61 | +window.title('Cryptocurrency Price Checker') |
| 62 | +window.geometry('1200x1000') |
| 63 | +window.configure(bg='white') |
| 64 | + |
| 65 | +style = ttk.Style() |
| 66 | +style.configure('my.TButton', font=('Helvetica', 16)) |
| 67 | +style.configure('my.TFrame', background='white') |
| 68 | + |
| 69 | +# label text for title |
| 70 | +ttk.Label(window, text="Cryptocurrency Price Checker", |
| 71 | + background='white', foreground="DodgerBlue2", |
| 72 | + font=("Helvetica", 30, 'bold')).grid(row=0, column=3,padx=300) |
| 73 | + |
| 74 | +submit_btn = ttk.Button(window, text="Fetch Live Price!", style='my.TButton', command = scraper) |
| 75 | +submit_btn.grid(row=5, column=3, pady=5, padx=15, ipadx=5) |
| 76 | + |
| 77 | +frame = ttk.Frame(window, style='my.TFrame') |
| 78 | +frame.place(relx=0.50, rely=0.12, relwidth=0.98, relheight=0.90, anchor="n") |
| 79 | + |
| 80 | +# To display stock data |
| 81 | +query_label = tk.Text(frame ,height="52" ,width="500", bg="lightskyblue1") |
| 82 | +query_label.grid(row=7, columnspan=2) |
| 83 | + |
| 84 | +window.mainloop() |
0 commit comments