zoukankan      html  css  js  c++  java
  • python爬虫快递查询系统(源码)


    import requests
    import json


    def 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, postid


    def 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()
  • 相关阅读:
    firefox配置
    安装gstreamer开发环境
    linux下批量替换文件内容(转)
    iptables详细教程:基础、架构、清空规则、追加规则、应用实例(转)
    iptables 使用
    如何用iptables实现NAT(转)
    Python 练习题
    Python unittest 参数化
    Python Logging模块
    Python 多进程
  • 原文地址:https://www.cnblogs.com/yunlongaimeng/p/9536553.html
Copyright © 2011-2022 走看看