• Shell借助curl调用接口结合Python解析Python案例


    需求说明:

    实现通过shell调用接口,并实现json的解析。

    解决方法:

     本案例按照两步来实现:

    1、借助curl命令调用接口,上传本地json文件

    2、对json文件利用python进行解析,生成结构化文本

    Step1:Shell通过curl调用接口

    1. #!/bin/bash
    2. input_file=/root/workbase/id_card.txt
    3. output_folder=/root/workbase/curl
    4. cat $input_file | while read myline
    5. do
    6. echo $myline
    7. token_wb=`curl --location 'http://192.168.56.2:7070/oauth/token?client_id=xw&client_secret=2w' --header 'Content-Type: application/json' --data '{}'`
    8. token2=$(echo $token_wb| awk -F ':' '{print $2}' |awk -F ',' '{print $1}'| sed 's/\"//g')
    9. filename=`echo $myline | sed "s/[\']//g"`
    10. curl -X POST --location 'http://192.168.56.2:7070/service/api/q2/dataexchangeserver/apiPayload/fe2/qx?client_id=fc&access_token='$token2'' --header 'Content-Type: application/json;charset=utf-8' --header 'Accept: application/json;charset=utf-8' --data '{"messageSequence":"01","condition":"ID='$myline' and STATUS='\''有效'\''","maxReturnNum":100,"orderParafs":{"ID":"desc"}}'| cat > $output_folder/$filename.json
    11. ##echo $filename
    12. ###sleep `expr $RANDOM % 2 + 1`
    13. done

    Step2:Python对Json进行解析

    1. def traverse_dir(path):
    2. try:
    3. files = glob.glob(os.path.join(path, "*"))
    4. for file in files:
    5. if os.path.isdir(file):
    6. print("文件夹:", file)
    7. traverse_dir(file)
    8. else:
    9. f = open(file, 'r', encoding='UTF8')
    10. data = json.load(f)
    11. with open('D:/household.txt', 'a', encoding='utf-8') as fw:
    12. for k,v in data['result'].items():
    13. if k == 'data':
    14. print(len(v['results']))
    15. for i in range(len(v['results'])):
    16. data = v['results'][i]
    17. NAME = data.get('NAME')
    18. ID = data.get('ID')
    19. STATUS = data.get('STATUS')
    20. ADDR = data.get('ADDR')
    21. str = NAME + '\t' + ID + '\t' + STATUS + '\t' + ADDR
    22. fw.write(str + '\n')
    23. except KeyError as e:
    24. print("There are missing fields in the user object: ", e)
    25. except FileNotFoundError as e:
    26. print("File is not found: ",e)
    27. except Exception:
    28. print("Error found \t", file)
    29. finally:
    30. f.close()
    31. fw.close()
    32. traverse_dir('D:\\household')

    #JSON实例样例:

    1. {
    2. "code": "0",
    3. "message": "查询成功",
    4. "result": {
    5. "status": 200,
    6. "msg": "正常",
    7. "messageSequence": "01",
    8. "data": {
    9. "totalRows": "1",
    10. "results": [
    11. {
    12. "NAME": "张三",
    13. "ID": "234513",
    14. "ADDR": "北京市创新大道1000号",
    15. "STATUS": "有效",
    16. }
    17. ]
    18. },
    19. "qtime": 1200
    20. }
    21. }

  • 相关阅读:
    戴建业作品集读书笔记
    刷爆力扣之检查数组对是否可以被 k 整除
    一个数组的异或和是指数组中所有的数异或在一起的结果,给定一个数组arr,求最大子数组异或和。
    烟雾、空气质量、温湿度…自己徒手做个环境检测设备
    layui laydate日期初始化的一些坑
    利用干扰源模型确定多通道音频信号盲源分离
    PAT 1017 Queueing at Bank
    深度学习代码入门test2_alexnet
    uniapp 添加定位权限及弹出权限弹框
    宝塔安装脚本
  • 原文地址:https://blog.csdn.net/shenliang1985/article/details/132914320