导语
随着网购的广泛普及,现在大部分年轻人都喜欢上了网购的方式。
很多东西物美价廉,出不出户也能满足你的购买需求!
尤其是中秋来临,哪些假期短回不了家的也想给家人带点儿中秋礼物~
这不?赶上中秋了,之前给家里寄东西的时候就出现过几次,物流信息一直没更新,不清楚东西到哪儿了,问卖家:说有时候上面没更新,但是到你家楼下了会打电话让你取快递的~
果然,emmmmmm,“打扰了!!”不知道你们遇到过没??
后来小编在一个专门全国查询快递的网站找到了物流信息23333,感觉这还是蛮实用的,至少快递也没丢撒!
今天的话木木子带大家写一款有界面的专属快递物流查询小系统~再也不用担心自己的快递突然消失啦!
正文
环境安装:
安装包-Python3、Pycharm2021、模块-requests、pyqt5。
pip install requests pip install pyqt5 #当然镜像源更快安装,环境问题直接找我
(1)首先导入所有快递公司的信息,这是以快递100为例的哈,之前爬的数据。
companies = pickle.load(open("companies.pkl", "rb"))
(2)利用快递100查询快递。
def getExpressInfo(number): url = "http://www.kuaidi100.com/autonumber/autoComNum?resultv2=1&text=%s" % number headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36", "Host": "www.kuaidi100.com" } infos = [] for each in requests.get(url, headers=headers).json()["auto"]: company_name = each["comCode"] url = "http://www.kuaidi100.com/query?type=%s&postid=%s" % (company_name, number) temps = requests.get(url, headers=headers).json()["data"] info = "公司: %s " % py2hz(company_name) for idx, each in enumerate(temps): if idx == 0: info += "-" * 60 + " 时间: " + each["time"] + " 进度: " + each["context"] + " " + "-" * 60 + " " else: info += "时间: " + each["time"] + " 进度: " + each["context"] + " " + "-" * 60 + " " if not temps: info += "-" * 60 + " " + "单号不存在或已过期 " + "-" * 60 + " " infos.append(info) return infos
(3)制作快递查询系统的界面。
class ExpressGUI(QWidget): def __init__(self, parent=None): super(ExpressGUI, self).__init__(parent) self.setWindowTitle("快递查询系统") self.label1 = QLabel("快递单号:") self.line_edit = QLineEdit() self.label2 = QLabel("查询结果:") self.text = QTextEdit() self.button = QPushButton() self.button.setText("查询") self.grid = QGridLayout() self.grid.setSpacing(12) self.grid.addWidget(self.label1, 1, 0) self.grid.addWidget(self.line_edit, 1, 1, 1, 39) self.grid.addWidget(self.button, 1, 40) self.grid.addWidget(self.label2, 2, 0) self.grid.addWidget(self.text, 2, 1, 1, 40) self.setLayout(self.grid) self.resize(600, 400) self.button.clicked.connect(self.inquiry) def inquiry(self): number = self.line_edit.text() try: infos = getExpressInfo(number) if not infos: infos = ["-" * 60 + " " + "单号不存在或已过期 " + "-" * 60 + " "] except: infos = ["-" * 60 + " " + "快递单号有误, 请重新输入. " + "-" * 60 + " "] self.text.setText(" ".join(infos)[:-1])
效果如下:
附源码:
""" Function: 快递查询系统 源码基地:#959755565# """ import sys import pickle import requests from PyQt5.QtWidgets import * """导入所有快递公司信息""" companies = pickle.load(open("companies.pkl", "rb")) """将快递公司的拼音变为汉字""" def py2hz(py): return companies.get(py) """利用快递100查询快递""" def getExpressInfo(number): url = "http://www.kuaidi100.com/autonumber/autoComNum?resultv2=1&text=%s" % number headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36", "Host": "www.kuaidi100.com" } infos = [] for each in requests.get(url, headers=headers).json()["auto"]: company_name = each["comCode"] url = "http://www.kuaidi100.com/query?type=%s&postid=%s" % (company_name, number) temps = requests.get(url, headers=headers).json()["data"] info = "公司: %s " % py2hz(company_name) for idx, each in enumerate(temps): if idx == 0: info += "-" * 60 + " 时间: " + each["time"] + " 进度: " + each["context"] + " " + "-" * 60 + " " else: info += "时间: " + each["time"] + " 进度: " + each["context"] + " " + "-" * 60 + " " if not temps: info += "-" * 60 + " " + "单号不存在或已过期 " + "-" * 60 + " " infos.append(info) return infos """制作简单的GUI""" class ExpressGUI(QWidget): def __init__(self, parent=None): super(ExpressGUI, self).__init__(parent) self.setWindowTitle("快递查询系统") self.label1 = QLabel("快递单号:") self.line_edit = QLineEdit() self.label2 = QLabel("查询结果:") self.text = QTextEdit() self.button = QPushButton() self.button.setText("查询") self.grid = QGridLayout() self.grid.setSpacing(12) self.grid.addWidget(self.label1, 1, 0) self.grid.addWidget(self.line_edit, 1, 1, 1, 39) self.grid.addWidget(self.button, 1, 40) self.grid.addWidget(self.label2, 2, 0) self.grid.addWidget(self.text, 2, 1, 1, 40) self.setLayout(self.grid) self.resize(600, 400) self.button.clicked.connect(self.inquiry) def inquiry(self): number = self.line_edit.text() try: infos = getExpressInfo(number) if not infos: infos = ["-" * 60 + " " + "单号不存在或已过期 " + "-" * 60 + " "] except: infos = ["-" * 60 + " " + "快递单号有误, 请重新输入. " + "-" * 60 + " "] self.text.setText(" ".join(infos)[:-1]) """run""" if __name__ == "__main__": app = QApplication(sys.argv) gui = ExpressGUI() gui.show() sys.exit(app.exec_())
总结
好啦!今天的文就写到这里!虽然还有一个小时!【开始摸鱼.jpg】因为明天周天休息呀!仰天长啸!
到此这篇关于我的快递一个月没动静于是赶紧上线python快递查询系统的文章就介绍到这了,更多相关python 快递查询内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_55822277/article/details/120238151