Prerequisite: BeautifulSoup, requests, tkinter, datetime
In this article, we are going to discuss how to get Gold and Silver Price in India Using Python in Tkinter. There is a variation in price with time to time, price of gold and silver is moved by a combination of supply, demand, and investor behavior.
We will use goodreturns website for fetching gold and silver price. We will create multiple functions for different operation:
- silver_price(): This function is used for getting the silver price. For silver, we will use this URL.
- gold_price(): This function is used for getting the gold price. For Gold, we will use this URL.
- get_number_from_string(): This function is used to extract integer or float from string.
- change_price(): This function is used to get the gold or silver price of a particular weight.
Step-by-step Approach:
Step1#
Import Required Libraries.
Python3
# Import Required libraryfrom bs4 import BeautifulSoupimport requestsfrom tkinter import *from datetime import datefrom tkinter import ttk |
Step2#
Get Silver and Gold Price.
Python3
# method to get the price of silverdef silver_price(): # getting the request from url data = requests.get( # converting the text soup = BeautifulSoup(data.text, 'html.parser') # finding metha info for the current price price = soup.find("div", class_="gold_silver_table right-align-content").find( "tr", class_="odd_row").findAll("td") # returnng the price in text return price[1].text # method to get the price of golddef gold_price(): # getting the request from url data = requests.get( # converting the text soup = BeautifulSoup(data.text, 'html.parser') # finding metha info for the current price price = soup.find("div", class_="gold_silver_table right-align-content").find( "tr", class_="odd_row").findAll("td") # returnng the price in text return price[1].text |
Step3#
Get Integer or float value from the string.
Python3
# Extract Integer or Float from Stringdef get_number_from_string(string): return float(''.join([x for x in string if x.isdigit() or x == '.'])) |
Step4#
Get the price of gold and silver for a particular weight.
Python3
# Get price for a particular weightdef change_price(weight_value): g_price = float(weight_value)*get_number_from_string(gold_price()) s_price = float(weight_value)*get_number_from_string(silver_price()) silver_price_label.config(text=f"{s_price} Rs") gold_price_label.config(text=f"{g_price} Rs") |
Below is the implementation based on the above approach:
Python3
# Import Required libraryfrom bs4 import BeautifulSoupimport requestsfrom tkinter import *from datetime import datefrom tkinter import ttk # Extract Integer or Float from Stringdef get_number_from_string(string): return float(''.join([x for x in string if x.isdigit() or x == '.'])) # Returns the current local datetoday = date.today() # method to get the price of silverdef silver_price(): # getting the request from url data = requests.get( # converting the text soup = BeautifulSoup(data.text, 'html.parser') # finding metha info for the current price price = soup.find("div", class_="gold_silver_table right-align-content").find( "tr", class_="odd_row").findAll("td") # returnng the price in text return price[1].text # method to get the price of golddef gold_price(): # getting the request from url data = requests.get( # converting the text soup = BeautifulSoup(data.text, 'html.parser') # finding metha info for the current price price = soup.find("div", class_="gold_silver_table right-align-content").find( "tr", class_="odd_row").findAll("td") # returnng the price in text return price[1].text # Get price for a particular weightdef change_price(weight_value): g_price = float(weight_value)*get_number_from_string(gold_price()) s_price = float(weight_value)*get_number_from_string(silver_price()) silver_price_label.config(text=f"{s_price} Rs") gold_price_label.config(text=f"{g_price} Rs") # Create Objectroot = Tk() # Set Geometryroot.geometry("400x400") # Create LabelLabel(root, text="SILVER AND GOLD PRICE", font=( "Helvetica 15 bold"), fg="blue").pack() # Frame1frame1 = Frame(root)frame1.pack(pady=20) Label(frame1, text="Today Date:- ", font=("Helvetica 15 bold")).pack(side=LEFT)Label(frame1, text=today, font=("Helvetica 15")).pack() # Frame2frame2 = Frame(root)frame2.pack(pady=20) # Set Variablevariable = StringVar(root)variable.set("1") Label(frame2, text="Select Weight:- ", font=("Helvetica 15 bold")).pack(side=LEFT)w = OptionMenu(frame2, variable, "1", "8", "100", "500", "1000", command=change_price)w.pack(side=LEFT)Label(frame2, text="gm", font=("Helvetica 15")).pack(side=LEFT) # Frame3frame3 = Frame(root)frame3.pack() Label(frame3, text="Silver Price:- ", font=("Helvetica 15 bold")).pack(side=LEFT)silver_price_label = Label(frame3, text="", font=("Helvetica 15"))silver_price_label.pack(pady=20) # Frame4frame4 = Frame(root)frame4.pack() Label(frame4, text="Gold Price:- ", font=("Helvetica 15 bold")).pack(side=LEFT)gold_price_label = Label(frame4, text="", font=("Helvetica 15"))gold_price_label.pack(pady=20) # Execute Tkinterroot.mainloop() |
Output:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


