欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python基于urllib的查快递工具

程序员文章站 2024-02-01 21:29:46
...

python基于urllib的查快递工具

import urllib.request
import json

def Check():
    while True:

        kd_num = input("请输入快递单号:")

        url_type = 'http://www.kuaidi100.com/autonumber/autoComNum?resultv2=1&text=%s' % (kd_num,)
        rs = urllib.request.urlopen(url_type)
        html_type = rs.read().decode('utf-8')
        kd_type_info = json.loads(html_type)
        kd_type = kd_type_info['auto'][0]['comCode']



        url = "http://www.kuaidi100.com/query?type=%s&postid=%s" % (kd_type, kd_num)
        response = urllib.request.urlopen(url)
        html = response.read().decode('utf-8')
        target = json.loads(html)
        #print(target)
        status = target['status']
        if status == '200':
            data = target['data']
            #print(data)
            data_len = len(data)
            #print(data_len)
            #print("\n")
            print("\n--------------------------------------------------------\n")
            #for i in range(data_len):
            for i in range(data_len-1,-1,-1):
            #for i in m:
                print("\n时间: " + data[i]['time'])
                print("状态: " + data[i]['context'] + "")
            print("\n--------------------------------------------------------\n")
            break
        else:
            print("输入有误请重新输入!\n")
    #print("按任意键结束......")


if __name__ == '__main__':
    while True:
        Check()
        out = input("按任意数字退出(其他键继续).........")
        if out >= '0' and out <= '9':
            break
        else:
            print("00000000000000000000000000000000\n")
            continue

2019年4月29日