python爬虫快递查询系统(源码)

元气小坏坏 提交于 2020-03-22 17:50:39

import requestsimport jsondef get_express_type(postid):    '''根据快递单号来智能判断快递类型'''    url = 'http://www.kuaidi100.com/autonumber/autoComNum?resultv2=1&text=%s' % (postid,)  # 这里可以用元组这样保证的数据的安全性    # 把构造后的url通过requests请求来得到相应的数据是一个json数据    rs = requests.get(url)    # 再用json库中的loads数据来进行分析得到一个可用字典的方式来访问    kd_type_info = json.loads(rs.text)    kd_type = kd_type_info['auto'][0]['comCode']    return kd_type, postiddef execute_data_query(type, postid):    '''执行数据查询程序'''    # 通过构造一个真正的url地址    url = 'http://www.kuaidi100.com/query?type=%s&postid=%s' % (type, postid)  # 这里可以用元组这样保证的数据的安全性    # 把构造后的url通过requests请求来得到相应的数据是一个json数据    rs = requests.get(url)    # 再用json库中的loads数据来进行分析得到一个可用字典的方式来访问    kd_info = json.loads(rs.text)    msg = kd_info['message']    # 判断是否成功获取到了json的数据,如果有数据则进行下一步的解析    if msg == 'ok':        print('您的快递%s物流信息如下:' % postid)        data = kd_info['data']        for data_dict in data:            time = data_dict['time']            context = data_dict['context']            print('时间:%s %s' % (time, context))    else:        if msg == '参数错误':            print('您输入信息有误,请重输:')        else:            print(msg)def main():    '''快递查询主程序'''    while True:        print('**欢迎您登录快递查询系统**')        print('-' * 30)        print('** 1. 请输入您的快递单号 **')        print('** 0. 退出查询系统       **')        print('-' * 30)        order = input('查询请输入1退出请输入0:')        if order == '1':            # 进行快递查询操作            postid = input('请输入您的快递单号:')            type, postid = get_express_type(postid)            execute_data_query(type, postid)        elif order == '0':            exit()        else:            print('!!!!!您的指令输入有误,请重新输入:<---------')if __name__ == '__main__':    main()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!