所需工具:
Python
聪明的大脑文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7482.html
勤劳的双手文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7482.html
文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7482.html
注意:本站只提供教程,不提供任何成品+工具+软件链接,仅限用于学习和研究。文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7482.html
文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7482.html
导语
随着网购的广泛普及,现在大部分年轻人都喜欢上了网购的方式。文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7482.html
很多东西物美价廉,出不出户也能满足你的购买需求!文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7482.html
尤其是中秋来临,哪些假期短回不了家的也想给家人带点儿中秋礼物~文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7482.html
这不?赶上中秋了,之前给家里寄东西的时候就出现过几次,物流信息一直没更新,不清楚东西到哪儿了,问卖家:说有时候上面没更新,但是到你家楼下了会打电话让你取快递的~文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7482.html
文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7482.html
果然,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\n' % py2hz(company_name)
- for idx, each in enumerate(temps):
- if idx == 0:
- info += '-' * 60 + '\n时间:\n' + each['time'] + '\n进度:\n' + each['context'] + '\n' + '-' * 60 + '\n'
- else:
- info += '时间:\n' + each['time'] + '\n进度:\n' + each['context'] + '\n' + '-' * 60 + '\n'
- if not temps:
- info += '-' * 60 + '\n' + '单号不存在或已过期\n' + '-' * 60 + '\n'
- 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 + '\n' + '单号不存在或已过期\n' + '-' * 60 + '\n']
- except:
- infos = ['-' * 60 + '\n' + '快递单号有误, 请重新输入.\n' + '-' * 60 + '\n']
- self.text.setText('\n\n\n'.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\n' % py2hz(company_name)
- for idx, each in enumerate(temps):
- if idx == 0:
- info += '-' * 60 + '\n时间:\n' + each['time'] + '\n进度:\n' + each['context'] + '\n' + '-' * 60 + '\n'
- else:
- info += '时间:\n' + each['time'] + '\n进度:\n' + each['context'] + '\n' + '-' * 60 + '\n'
- if not temps:
- info += '-' * 60 + '\n' + '单号不存在或已过期\n' + '-' * 60 + '\n'
- 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 + '\n' + '单号不存在或已过期\n' + '-' * 60 + '\n']
- except:
- infos = ['-' * 60 + '\n' + '快递单号有误, 请重新输入.\n' + '-' * 60 + '\n']
- self.text.setText('\n\n\n'.join(infos)[:-1])
- '''run'''
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- gui = ExpressGUI()
- gui.show()
- sys.exit(app.exec_())
评论