Python制作快递查询系统

时间:2024-04-12 08:53:53

代码

import json
from tkinter import *
import requests
def getType(postId):
    url='http://www.kuaidi100.com/autonumber/autoComNum?resultv2=1&text='+postId
    rs=requests.get(url)
    postTypeInfo=json.loads(rs.text)
    print(postTypeInfo['comCode'])
    if not postTypeInfo['auto']:
        postType='xxx'
    else:
        postType=postTypeInfo['auto'][0]['comCode']
    return postType
def getMessage(postType,postId):
    if postType=='xxx':
        messageAll='请输入正确的快递单号'
    else:
        url='http://www.kuaidi100.com/query?type='+postType+'&postid='+postId
        rs=requests.get(url)
        messageInfo=json.loads(rs.text)
        if messageInfo['message']=='ok':
            messageAll='   快递单号:'+messageInfo['nu']+'\n'\
                       +'   快递公司:'+messageInfo['com']+'\n'\
                       +'   快递信息:'+'\n'
            dataAll=messageInfo['data']
            for item in dataAll:
                messageAll=messageAll+'   时间:'+item['time']+'\n'\
                            +'        '+item['context']+'\n'
        else:
            messageAll=messageInfo['message']
    return messageAll
def action(postId,e,e1):
    e1.set(getMessage(getType(e.get()), e.get()))
def main():
    postId='88307061538'
    #print(getMessage(getType(postId),postId))
    root=Tk()
    root.title('快递查询')
    root.minsize(500,500)
    e=StringVar()
    e.set('请输入快递单号')
    e1=StringVar()
    entry=Entry(root,bg='#ffffff',width=30,textvariable=e).place(x=30,y=30,anchor='nw')
    message = Message(root,textvariable=e1,width=377).place(x=30, y=70, anchor='nw')
    bt=Button(root,bg='white',text='查询',width=10,height=1,command=lambda :action(postId,e,e1)).place(x=320,y=26,anchor='nw')
    root.mainloop()
main()

运行结果

Python制作快递查询系统