Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e91c8cd

Browse files
Merge pull request avinashkranjan#943 from Ayushjain2205/NSE-stocks-GUI
Nse stocks GUI
2 parents 12d7ec3 + 947285f commit e91c8cd

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

‎NSE Stocks GUI/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# NSE Stock Data
2+
3+
Running this Script would allow the user to go through NSE Stock data scraped from [NSE Website](https://www.nseindia.com), based on their choice of available categories.
4+
5+
## Setup instructions
6+
7+
In order to run this script, you need to have Python and pip installed on your system. After you're done installing Python and pip, run the following command from your terminal to install the requirements from the same folder (directory) of the project.
8+
9+
```
10+
pip install -r requirements.txt
11+
```
12+
13+
As this script uses selenium, you will need to install the chrome webdriver from [this link](https://sites.google.com/a/chromium.org/chromedriver/downloads)
14+
15+
After satisfying all the requirements for the project, Open the terminal in the project folder and run
16+
17+
```
18+
python stocks.py
19+
```
20+
21+
or
22+
23+
```
24+
python3 stocks.py
25+
```
26+
27+
depending upon the python version. Make sure that you are running the command from the same virtual environment in which the required modules are installed.
28+
29+
## Output
30+
31+
The user can select different categories from the GUI as shown in the sample screen shot below
32+
33+
![Screenshot of the GUI](https://i.postimg.cc/FRnNzQMP/nse.png)
34+
35+
36+
## Author
37+
38+
[Ayush Jain](https://github.com/Ayushjain2205)

‎NSE Stocks GUI/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
requests
2+
beautifulsoup4
3+
selenium

‎NSE Stocks GUI/stocks.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /