PyQt5 – 获取比特币价格的应用程序
在这篇文章中,我们将看到如何制作一个告诉比特币实时价格的PyQt5应用程序。 需要的模块和安装: PyQt5: PyQt5是跨平台GUI工具包Qt的一个Python绑定,作为一个Python插件实现。PyQt5是由英国公司Riverbank Computing开发的免费软件。
pip install PyQt5
请求: 请求允许你非常容易地发送HTTP/1.1请求。不需要手动添加查询字符串到你的URLs。
pip install requests
Beautiful Soup: Beautiful Soup是一个库,它使从网页上搜刮信息变得容易。它位于HTML或XML解析器之上,为迭代、搜索和修改解析树提供Pythonic习语。
pip install beautifulsoup4
制作获取比特币实时价格的应用程序的步骤 –
1.创建一个窗口并设置其尺寸和标题
2.创建一个标签,说明价格如何
3.创建一个按钮
4.在按钮上添加动作
5.在动作的帮助下,获取比特币价格的数据
6.使用beautiful soup将数据转换成html代码,找到价格并将其保存在变量中
7.改变标签的文本,将价格设为文本
以下是执行情况 –
# importing libraries
from bs4 import BeautifulSoup as BS
import requests
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
# setting title
self.setWindowTitle("PyQt5 Bit Coin ")
# setting geometry
self.setGeometry(100, 100, 400, 600)
# calling method
self.UiComponents()
# showing all the widgets
self.show()
# method for widgets
def UiComponents(self):
# creating label to show bit coin price
self.label = QLabel("//BIT-COIN//", self)
# setting geometry of label
self.label.setGeometry(50, 150, 300, 50)
# setting its alignment
self.label.setAlignment(Qt.AlignCenter)
# setting font to the label
self.label.setFont(QFont('Times', 15))
# setting border to the push button
self.label.setStyleSheet("border : 3px solid black;")
# creating push button to show current price
button = QPushButton("Show price", self)
# setting geometry to push button
button.setGeometry(150, 300, 100, 40)
# adding method to the push button
button.clicked.connect(self.show_price)
# setting tool tip to button
button.setToolTip("Press to get Bit Coin Price")
def show_price(self):
# url of the bit coin price
url = "https://www.google.com/search?q = bitcoin + price"
# getting the request from url
data = requests.get(url)
# converting the text
soup = BS(data.text, 'html.parser')
# finding meta info for the current price
ans = soup.find("div", class_="BNeawe iBp4i AP7Wnd").text
# setting this price to the label
self.label.setText(ans)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())