In this article, we are going to write a python scripts to get live information of USD/INR rate and bind with it GUI application.
Modules Required:
- bs4: Beautiful Soup is a Python library for pulling data out of HTML and XML files.
Installation:
pip install bs4
- requests: This module allows you to send HTTP/1.1 requests very easily.
Installation:
pip install requests
Step-by-step Approach:
- Extract data form given URL. Copy the URL, after selecting the desired location.
- Scrape the data with the help of requests and Beautiful Soup module.
- Convert that data into HTML code.
- Find the required details and filter them.
Implementation:
Step 1: Import all the modules required.
Python3
# Import required modules import requests from bs4 import BeautifulSoup |
Step 2: Create a URL get function
Python3
# Function to extract html data def getdata(url): r = requests.get(url) return r.text |
Step 3: Now pass the URL into the getdata() function and convert that data(currency details) into HTML code.
The URL used here is https://finance.yahoo.com/quote/usdinr=X?ltr=1
Python3
# Extract and convert soup = BeautifulSoup(htmldata, 'html.parser' ) result = (soup.find_all( "div" , class_ = "D(ib) Va(m) Maw(65%) Ov(h)" ) |
Output:
Step 4: Filter the currency details and quality(increment/decrement) according to the given data.
Python3
mydatastr = '' # Filter converted data for table in soup.find_all( "div" , class_ = "D(ib) Va(m) Maw(65%) Ov(h)" ): mydatastr + = table.get_text() # Display output print (mydatastr) |
Output:
'73.2610-0.2790 (-0.38%)As of 3:30PM BST. Market open.'
Below is the complete program implemented using tkinter module.
Python
# Import required modules from tkinter import * import requests from bs4 import BeautifulSoup # user defined funtion # to extract currency details def getdata(url): r = requests.get(url) return r.text # Function to compute and display currency detalis def get_info(): try : soup = BeautifulSoup(htmldata, 'html.parser' ) mydatastr = '' for table in soup.find_all( "div" , class_ = "D(ib) Va(m) Maw(65%) Ov(h)" ): mydatastr + = table.get_text() list_data = mydatastr.split() temp = list_data[ 0 ].split( "-" ) rate. set (temp[ 0 ]) inc. set (temp[ 1 ]) per_rate. set (list_data[ 1 ]) time. set (list_data[ 3 ]) result. set ( "success" ) except : result. set ( "Opps! someting wrong" ) # Driver Code # Create tkinter object master = Tk() # Set background color master.configure(bg = 'light grey' ) # Variable Classes in tkinter result = StringVar() rate = StringVar() per_rate = StringVar() time = StringVar() inc = StringVar() # Creating label for each information Label(master, text = "Status :" , bg = "light grey" ).grid(row = 2 , sticky = W) Label(master, text = "Current rate of INR :" , bg = "light grey" ).grid(row = 3 , sticky = W) Label(master, text = "Increase/decrease by :" , bg = "light grey" ).grid(row = 4 , sticky = W) Label(master, text = "Rate change :" , bg = "light grey" ).grid(row = 5 , sticky = W) Label(master, text = "Rate of time :" , bg = "light grey" ).grid(row = 6 , sticky = W) # Creating lebel for class variable Label(master, text = "", textvariable = result, bg = "light grey" ).grid(row = 2 , column = 1 , sticky = W) Label(master, text = "", textvariable = rate, bg = "light grey" ).grid(row = 3 , column = 1 , sticky = W) Label(master, text = " ", textvariable=inc, bg=" light grey").grid( row = 4 , column = 1 , sticky = W) Label(master, text = "", textvariable = per_rate, bg = "light grey" ).grid(row = 5 , column = 1 , sticky = W) Label(master, text = "", textvariable = time, bg = "light grey" ).grid(row = 6 , column = 1 , sticky = W) # Create submit button b = Button(master, text = "Show" , command = get_info, bg = "Blue" ).grid(row = 0 ) 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.