|
| 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 path for chromedriver: ') |
| 11 | + |
| 12 | +# Categories and their URL slugs |
| 13 | +most_active = {'Most Active equities - Main Board':'mae_mainboard_tableC','Most Active equities - SME':'mae_sme_tableC','Most Active equities - ETFs':'mae_etf_tableC', |
| 14 | + 'Most Active equities - Price Spurts':'mae_pricespurts_tableC', 'Most Active equities - Volume Spurts':'mae_volumespurts_tableC'} |
| 15 | +top_20 = {'NIFTY 50 Top 20 Gainers':'topgainer-Table','NIFTY 50 Top 20 Losers':'toplosers-Table'} |
| 16 | + |
| 17 | +# Function to generate request url based on user choice |
| 18 | +def generate_url(): |
| 19 | + category_choice = category.get() |
| 20 | + if(category_choice in most_active): |
| 21 | + page = 'most-active-equities' |
| 22 | + else: |
| 23 | + page = 'top-gainers-loosers' |
| 24 | + url = 'https://www.nseindia.com/market-data/{}'.format(page) |
| 25 | + return url |
| 26 | + |
| 27 | +# Function to scrape stock data from generated URL |
| 28 | +def scraper(): |
| 29 | + url = generate_url() |
| 30 | + driver = webdriver.Chrome(driver_path) |
| 31 | + driver.get(url) |
| 32 | + |
| 33 | + # Wait for results to load |
| 34 | + time.sleep(5) |
| 35 | + html = driver.page_source |
| 36 | + |
| 37 | + # Start scraping resultant html data |
| 38 | + soup = BeautifulSoup(html, 'html.parser') |
| 39 | + |
| 40 | + # Based on choice scrape div |
| 41 | + category_choice = category.get() |
| 42 | + if category_choice in most_active : |
| 43 | + category_div = most_active[category_choice] |
| 44 | + else : |
| 45 | + category_div = top_20[category_choice] |
| 46 | + |
| 47 | + # Find the table to scrape |
| 48 | + results = soup.find("table", {"id": category_div}) |
| 49 | + rows = results.findChildren('tr') |
| 50 | + |
| 51 | + table_data = [] |
| 52 | + row_values = [] |
| 53 | + # Append stock data into a list |
| 54 | + for row in rows: |
| 55 | + cells = row.findChildren(['th', 'td']) |
| 56 | + for cell in cells: |
| 57 | + value = cell.text.strip() |
| 58 | + value = " ".join(value.split()) |
| 59 | + row_values.append(value) |
| 60 | + table_data.append(row_values) |
| 61 | + row_values = [] |
| 62 | + |
| 63 | + # Formatting the stock data stored in the list |
| 64 | + stocks_data = "" |
| 65 | + for stock in table_data: |
| 66 | + single_record = "" |
| 67 | + for cell in stock: |
| 68 | + format_cell = "{:<20}" |
| 69 | + single_record += format_cell.format(cell[:20]) |
| 70 | + single_record += "\n" |
| 71 | + stocks_data += single_record |
| 72 | + |
| 73 | + # Adding the formatted data into tkinter GUI |
| 74 | + query_label.config(state=tk.NORMAL) |
| 75 | + query_label.delete(1.0,"end") |
| 76 | + query_label.insert(1.0,stocks_data) |
| 77 | + query_label.config(state=tk.DISABLED) |
| 78 | + driver.close() |
| 79 | + |
| 80 | +# Creating tkinter window |
| 81 | +window = tk.Tk() |
| 82 | +window.title('NSE Stock data') |
| 83 | +window.geometry('1200x1000') |
| 84 | +window.configure(bg='white') |
| 85 | + |
| 86 | +style = ttk.Style() |
| 87 | +style.configure('my.TButton', font=('Helvetica', 16)) |
| 88 | +style.configure('my.TFrame', background='white') |
| 89 | + |
| 90 | +# label text for title |
| 91 | +ttk.Label(window, text="NSE Stock market data", |
| 92 | + background='white', foreground="SpringGreen2", |
| 93 | + font=("Helvetica", 30, 'bold')).grid(row=0, column=1) |
| 94 | + |
| 95 | +# label |
| 96 | +ttk.Label(window, text="Select Market data to get:", background = 'white', |
| 97 | + font=("Helvetica", 15)).grid(column=0, |
| 98 | + row=5, padx=10, pady=25) |
| 99 | + |
| 100 | +# Combobox creation |
| 101 | +category = ttk.Combobox( |
| 102 | + window, width=60, state='readonly',font="Helvetica 15") |
| 103 | + |
| 104 | +submit_btn = ttk.Button(window, text="Get Stock Data!", style='my.TButton', command = scraper) |
| 105 | + |
| 106 | +# Adding combobox drop down list |
| 107 | +category['values'] = ('Most Active equities - Main Board','Most Active equities - SME','Most Active equities - ETFs','Most Active equities - Price Spurts', |
| 108 | + 'Most Active equities - Volume Spurts','NIFTY 50 Top 20 Gainers','NIFTY 50 Top 20 Losers') |
| 109 | + |
| 110 | +category.grid(column=1, row=5, padx=10) |
| 111 | +category.current(0) |
| 112 | + |
| 113 | +submit_btn.grid(row=5, column=3, pady=5, padx=15, ipadx=5) |
| 114 | + |
| 115 | +frame = ttk.Frame(window, style='my.TFrame') |
| 116 | +frame.place(relx=0.50, rely=0.12, relwidth=0.98, relheight=0.90, anchor="n") |
| 117 | + |
| 118 | +# To display stock data |
| 119 | +query_label = tk.Text(frame ,height="52" ,width="500", bg="alice blue") |
| 120 | +query_label.grid(row=7, columnspan=2) |
| 121 | + |
| 122 | +window.mainloop() |
0 commit comments