使用Python获取实时美元/印度卢比汇率的应用程序
在本文中,我们将编写一个Python脚本来获取美元/印度卢比汇率的实时信息并与其绑定 GUI 应用程序。
所需模块:
- bs4: Beautiful Soup是一个Python库,用于从HTML和XML文件中提取数据。
安装:
pip install bs4
- 请求:该模块允许您非常轻松地发送HTTP/1.1请求。
安装:
pip install requests
循序渐进的方法:
- 从给定的 URL 中提取数据。选择所需位置后复制 URL。
- 在请求和 Beautiful Soup 模块的帮助下抓取数据。
- 将该数据转换为 HTML 代码。
- 找到所需的详细信息并过滤它们。
执行:
第 1 步:导入所有需要的模块。
Python3
# Import required modules
import requests
from bs4 import BeautifulSoup
Python3
# Function to extract html data
def getdata(url):
r=requests.get(url)
return r.text
Python3
# Extract and convert
htmldata = 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)")
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)
Python
# Import required modules
from tkinter import *
import requests
from bs4 import BeautifulSoup
# user defined function
# 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:
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! 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()
第 2 步:创建 URL 获取函数
蟒蛇3
# Function to extract html data
def getdata(url):
r=requests.get(url)
return r.text
第 3 步:现在将 URL 传递到getdata()函数并将该数据(货币详细信息)转换为HTML代码。
这里使用的 URL 是 https://finance.yahoo.com/quote/usdinr=X?ltr=1
蟒蛇3
# Extract and convert
htmldata = 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)")
输出:
第四步:根据给定的数据过滤货币明细和质量(增量/减量)。
蟒蛇3
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)
输出:
'73.2610-0.2790 (-0.38%)As of 3:30PM BST. Market open.'
下面是使用tkinter模块实现的完整程序。
Python
# Import required modules
from tkinter import *
import requests
from bs4 import BeautifulSoup
# user defined function
# 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:
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! 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()
输出: