python制作快递查询小软件

用python制作快递查询小软件,使用Pycharm+Python+PyQt5

快递查询实现思路

  • 通过对网址http://www.kuaidi100.com/ 抓包,发现通过self.url1 = r'http://www.kuaidi100.com/autonumber/autoComNum?text={}' {}内为快递单号,通过这个链接判断可能的快递公司。
  • 通过查询出来的快递公司代码,与快递单号,拼接成网址self.url2 = r'http://www.kuaidi100.com/query?type={}&postid={}' 来查询物流信息

Pycharm+Python+PyQt5 安装和配置

Pycharm+Python+PyQt5 安装和配置

如何将PyQt(pyqt-tools)中的Qt Designer改为中文界面(汉化)

如何将PyQt(pyqt-tools)中的Qt Designer改为中文界面(汉化)

pyinstaller库的使用

https://blog.csdn.net/i_chaoren/article/details/56019823

参考

https://blog.csdn.net/weixin_41929524/article/details/81456308

代码

  • UI代码 kdcx_ui.py
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'kdcx_ui.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(756, 309)
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(22, 32, 48, 20))
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(76, 32, 256, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(22, 70, 48, 20))
        self.label_2.setObjectName("label_2")
        self.textBrowser = QtWidgets.QTextBrowser(Form)
        self.textBrowser.setGeometry(QtCore.QRect(76, 70, 631, 192))
        self.textBrowser.setLineWrapMode(QtWidgets.QTextEdit.NoWrap)
        self.textBrowser.setObjectName("textBrowser")
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(440, 30, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(560, 30, 75, 23))
        self.pushButton_2.setObjectName("pushButton_2")
        self.label_3 = QtWidgets.QLabel(Form)
        self.label_3.setGeometry(QtCore.QRect(80, 279, 121, 21))
        self.label_3.setText("")
        self.label_3.setObjectName("label_3")

        self.retranslateUi(Form)
        self.pushButton_2.clicked.connect(Form.close)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "快递查询"))
        self.label.setText(_translate("Form", "快递单号"))
        self.label_2.setText(_translate("Form", "查询结果"))
        self.pushButton.setText(_translate("Form", "查询"))
        self.pushButton_2.setText(_translate("Form", "退出"))

  • 快递查询实现代码 kdcx.py
from PyQt5 import QtWidgets
from kdcx_ui import Ui_Form
import requests
import json

class mywindow(QtWidgets.QWidget, Ui_Form):
    def  __init__ (self):
        super(mywindow, self).__init__()
        self.setupUi(self)
        self.url1 = r'http://www.kuaidi100.com/autonumber/autoComNum?text={}'
        self.url2 = r'http://www.kuaidi100.com/query?type={}&postid={}'
        self.print_context = r'公司:{}  时间: {}  内容:{}'
        self.pushButton.clicked.connect(self.run)

    def paser_url(self,url):
        resp = requests.get(url).content.decode()
        return json.loads(resp)

    def run(self):
        self.label_3.setText("查询中...")
        postid = self.lineEdit.text()
        url = self.url1.format(postid)
        resp = self.paser_url(url)
        com_code = []
        content_list = []

        if len(resp["auto"]) != 0:
            for gs in resp["auto"]:
                com_code.append(gs["comCode"])

        if len(com_code) == 0:
            pass

        else:
            for com in com_code:
                url2 = self.url2.format(com,postid)
                resp2 = self.paser_url(url2)
                if resp2["status"] == '200':


                    for i in range(len(resp2["data"])-1,-1,-1):
                        content = self.print_context.format(resp2["com"],resp2["data"][i]["ftime"],resp2["data"][i]["context"])
                        content_list.append(content)
                    content_list.append("*"*100)
                    content_list.append("*" * 100)
        if len(content_list) == 0:
            content_list.append("单号错误,或订单过期,找不到数据!")
        self.textBrowser.setText("\n".join(content_list))
        self.label_3.setText("查询完成!")





if __name__=="__main__":
    import sys
    app=QtWidgets.QApplication(sys.argv)
    ui = mywindow()
    ui.show()
    sys.exit(app.exec_())


你可能感兴趣的:(python制作快递查询小软件)