Application to get live USD/INR rate Using Python - GeeksforGeeks

04 Dec, 2021
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.
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
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.
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 converthtmldata = getdata("https://finance.yahoo.com/quote/usdinr=X?ltr=1")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 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:        htmldata = getdata("https://finance.yahoo.com/quote/usdinr=X?ltr=1")        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! something 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 label 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:
abhigoya
akshaysingh98088
simmytarika5
nnr223442
Python Tkinter-exercises
Python-tkinter
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Selecting rows in pandas DataFrame based on conditions
Create a directory in Python
Python | Pandas dataframe.groupby()