Application to get live USD/INR rate Using Python
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 from the given URL. Copy the URL, after selecting the desired location.
- Scrape the data with the help of requests and the 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 modulesimport requestsfrom bs4 import BeautifulSoup |
Step 2: Create a URL get function
Python3
# Function to extract html datadef 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 convertsoup = 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 datafor table in soup.find_all("div", class_="D(ib) Va(m) Maw(65%) Ov(h)"): mydatastr += table.get_text()# Display outputprint(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 modulesfrom tkinter import *import requestsfrom bs4 import BeautifulSoup# user defined function# to extract currency detailsdef getdata(url): r = requests.get(url) return r.text # Function to compute and display currency detalisdef 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 objectmaster = Tk()# Set background colormaster.configure(bg='light grey')# Variable Classes in tkinterresult = StringVar()rate = StringVar()per_rate = StringVar()time = StringVar()inc = StringVar()# Creating label for each informationLabel(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 variableLabel(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 buttonb = 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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course



