PyQt5实时汇率查询

用PyQt5实现了界面,使用urllib实时抓取ip138.com网站的汇率信息。

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_()


你可能感兴趣的:(python,urllib,PyQt)