PyQt5实时汇率查询
用PyQt5实现了界面,使用urllib实时抓取ip138.com网站的汇率信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | import sys import urllib import urllib.request from PyQt5.QtWidgets import * class Form(QDialog): def __init__( self , parent = None ): super (Form, self ).__init__(parent) self .getdata() comboItems = list ( self .rates.keys()) textLabel = QLabel( '实时汇率查询换算' ) self .fromComboBox = QComboBox() self .fromComboBox.addItems(comboItems) self .fromSpinBox = QDoubleSpinBox() self .fromSpinBox.setRange( 0.01 , 10000000.00 ) self .fromSpinBox.setValue( 100 ) self .toComboBox = QComboBox() self .toComboBox.addItems(comboItems) self .toLabel = QLabel( '1.00' ) grid = QGridLayout() grid.addWidget(textLabel, 0 , 0 ) grid.addWidget( self .fromComboBox, 1 , 0 ) grid.addWidget( self .fromSpinBox, 1 , 1 ) grid.addWidget( self .toComboBox, 2 , 0 ) grid.addWidget( self .toLabel, 2 , 1 ) self .setLayout(grid) self .updateUi() self .fromComboBox.currentIndexChanged.connect( self .updateUi) self .toComboBox.currentIndexChanged.connect( self .updateUi) self .fromSpinBox.valueChanged.connect( self .updateUi) self .setWindowTitle( '汇率 @author : likui' ) def updateUi( self ): from_value = self .rates[ self .fromComboBox.currentText()] to_value = self .rates[ self .toComboBox.currentText()] money = self .fromSpinBox.value() url = 'http://qq.ip138.com/hl.asp?from=%s&to=%s&q=%s' % ( from_value, to_value, money) html = self .urlopen(url) table_start = html.find( '<table' ) count = 5 td_begin = html.find( '<td' ) while count: count - = 1 td_begin = html.find( '<td' , td_begin + 1 ) temp = td_begin td_end = html.find( '</td>' , td_begin) amount = float (html[td_begin + 4 :td_end]) self .toLabel.setText( '%0.2f' % amount) def getdata( self ): self .rates = {} url = 'http://qq.ip138.com/hl.asp' html = self .urlopen(url) td_begin = html.find( '<td>' ) td_end = html.find( '</td>' , td_begin) resource = html[td_begin:td_end] begin = resource.find( '<optgroup' ) end = resource.find( '</optgroup>' ) option_begin = resource.find( '<option' , begin) while option_begin ! = - 1 : value = resource[option_begin + 14 :option_begin + 17 ] value_begin = resource.find( '>' , option_begin) value_end = resource.find( '<' , value_begin) rate = resource[value_begin + 1 :value_end] self .rates[rate] = value option_begin = resource.find( '<option' , value_end) if option_begin > end: break def urlopen( self , url): ret = urllib.request.Request(url) ret.add_header( 'User-Agent' , 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/41.0.2272.101 Safari/537.36' ) html = urllib.request.urlopen(ret).read().decode( 'GB2312' ) return html if __name__ = = '__main__' : app = QApplication(sys.argv) form = Form() form.show() app.exec_() |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库