Requisito previo: BeautifulSoup , requests , tkinter , fecha y hora
En este artículo, vamos a discutir cómo obtener el precio del oro y la plata en la India usando Python en Tkinter. Hay una variación en el precio con el tiempo, el precio del oro y la plata se mueve por una combinación de oferta, demanda y comportamiento de los inversores.
Usaremos el sitio web de goodreturns para obtener el precio del oro y la plata. Crearemos múltiples funciones para diferentes operaciones:
- silver_price(): Esta función se utiliza para obtener el precio de la plata. Para plata, usaremos esta URL .
- gold_price(): Esta función se utiliza para obtener el precio del oro. Para Gold, usaremos esta URL .
- get_number_from_string(): esta función se usa para extraer números enteros o flotantes de una string.
- change_price(): Esta función se utiliza para obtener el precio del oro o la plata de un peso en particular.
Enfoque paso a paso:
Paso 1#
Importar bibliotecas requeridas.
Python3
# Import Required library from bs4 import BeautifulSoup import requests from tkinter import * from datetime import date from tkinter import ttk
Paso 2#
Obtenga precio de plata y oro.
Python3
# method to get the price of silver def silver_price(): # getting the request from url data = requests.get( "https://www.goodreturns.in/silver-rates/#Today+24+Carat+Gold+Rate+Per+Gram+in+India+%28INR%29") # 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") # returning the price in text return price[1].text # method to get the price of gold def gold_price(): # getting the request from url data = requests.get( "https://www.goodreturns.in/gold-rates/#Today+24+Carat+Gold+Rate+Per+Gram+in+India+%28INR%29") # 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") # returning the price in text return price[1].text
Paso 3#
Obtenga un valor entero o flotante de la string.
Python3
# Extract Integer or Float from String def get_number_from_string(string): return float(''.join([x for x in string if x.isdigit() or x == '.']))
Paso 4#
Obtenga el precio del oro y la plata por un peso particular.
Python3
# Get price for a particular weight def 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")
A continuación se muestra la implementación basada en el enfoque anterior:
Python3
# Import Required library from bs4 import BeautifulSoup import requests from tkinter import * from datetime import date from tkinter import ttk # Extract Integer or Float from String def get_number_from_string(string): return float(''.join([x for x in string if x.isdigit() or x == '.'])) # Returns the current local date today = date.today() # method to get the price of silver def silver_price(): # getting the request from url data = requests.get( "https://www.goodreturns.in/silver-rates/#Today+24+Carat+Gold+Rate+Per+Gram+in+India+%28INR%29") # 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") # returning the price in text return price[1].text # method to get the price of gold def gold_price(): # getting the request from url data = requests.get( "https://www.goodreturns.in/gold-rates/#Today+24+Carat+Gold+Rate+Per+Gram+in+India+%28INR%29") # 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") # returning the price in text return price[1].text # Get price for a particular weight def 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 Object root = Tk() # Set Geometry root.geometry("400x400") # Create Label Label(root, text="SILVER AND GOLD PRICE", font=( "Helvetica 15 bold"), fg="blue").pack() # Frame1 frame1 = 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() # Frame2 frame2 = Frame(root) frame2.pack(pady=20) # Set Variable variable = 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) # Frame3 frame3 = 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) # Frame4 frame4 = 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 Tkinter root.mainloop()
Producción: