父子节点内容和个数提取

有时我们需要获得菜单的内容和个数,这个时候通常有父子菜单,那么怎么分别获取到他们呢?以下面的智慧物业管理系统为例,有7个父节点,每个父节点下面有子节点。如何把父节点名称和总数,以及子节点的名称和总数提取出来呢?

1、抓取返回值

首先我们应该先抓取返回值

可以放notepad++里面,

也可以放pycharm里面查看

2、常量获取对应的父子节点的名称和父子节点的总数试验V1

 1、首先提取getrouters接口的返回内容

import requests

# 定义接口地址
url='https://svr-6-9009.share.51env.net/prod-api/getRouters'

# 定义token参数
token="eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImM2YjI1OGMzLTRlZDQtNGM5OS05YWYwLTgyOTc1ZDY0MzcxZiJ9.FRX40yX-9hdENLARAn5N_n9JtlGsKQRbTNmKsJBu0GX70RRVUR2pvAjBeFSv61lHy-tzJdLj5P3HPsdtsjw3yg"
header={'Authorization':'Bearer '+token}
# 发送请求并获取
response=requests.get(url,headers=header).json()
print(response)

输出结果:

{'msg': '操作成功', 'code': 200, 'data': [{'name': 'Payment', 'path': '/payment', 'hidden': False, 'redirect': 'noRedirect', 'component': 'Layout', 'alwaysShow': True, 'meta': {'title': '物业收费', 'icon': 'shopping', 'noCache': False, 'link': None}, 'children': [{'name': 'Housepay', 'path': 'housepay', 'hidden': False, 'component': 'pms/housepay', 'meta': {'title': '商铺综合收费', 'icon': 'money', 'noCache': False, 'link': None}}, {'name': 'Temppay', 'path': 'temppay', 'hidden': False, 'component': 'pms/paymenttemp', 'meta': {'title': '临时收费', 'icon': 'form', 'noCache': False, 'link': None}}, {'name': 'Deposit', 'path': 'deposit', 'hidden': False, 'component': 'pms/paymentdeposit', 'meta': {'title': '押金管理', 'icon': 'dict', 'noCache': False, 'link': None}}, {'name': 'Pre', 'path': 'pre', 'hidden': False, 'component': 'pms/paymentpre', 'meta': {'title': '预存款管理', 'icon': 'documentation', 'noCache': False, 'link': None}}]}, {'name': 'Fee', 'path': '/fee', 'hidden': False, 'redirect': 'noRedirect', 'component': 'Layout', 'alwaysShow': True, 'meta': {'title': '收费数据管理', 'icon': 'build', 'noCache': False, 'link': None}, 'children': [{'name': 'Meter', 'path': 'meter', 'hidden': False, 'component': 'pms/paymentmeter', 'meta': {'title': '抄表数据管理', 'icon': 'time-range', 'noCache': False, 'link': None}}, {'name': 'Bill', 'path': 'bill', 'hidden': False, 'component': 'pms/paymentbill', 'meta': {'title': '商铺收费数据', 'icon': 'edit', 'noCache': False, 'link': None}}]}, {'name': 'Config', 'path': '/config', 'hidden': False, 'redirect': 'noRedirect', 'component': 'Layout', 'alwaysShow': True, 'meta': {'title': '基础数据管理', 'icon': 'cascader', 'noCache': False, 'link': None}, 'children': [{'name': 'Configfeeitem', 'path': 'configfeeitem', 'hidden': False, 'component': 'pms/configfeeitem', 'meta': {'title': '收费项管理', 'icon': 'list', 'noCache': False, 'link': None}}, {'name': 'Pms/confighouseblock', 'path': 'pms/confighouseblock', 'hidden': False, 'component': 'pms/confighouseblock', 'meta': {'title': '商业区管理', 'icon': 'example', 'noCache': False, 'link': None}}, {'name': 'Config/house', 'path': 'config/house', 'hidden': False, 'component': 'pms/confighouse', 'meta': {'title': '商铺管理', 'icon': 'component', 'noCache': False, 'link': None}}, {'name': 'Config/contract', 'path': 'config/contract', 'hidden': False, 'component': 'pms/confighousecontract', 'meta': {'title': '商铺租售', 'icon': 'switch', 'noCache': False, 'link': None}}]}, {'name': 'Statistics', 'path': '/statistics', 'hidden': False, 'redirect': 'noRedirect', 'component': 'Layout', 'alwaysShow': True, 'meta': {'title': '统计报表', 'icon': 'monitor', 'noCache': False, 'link': None}, 'children': [{'name': 'DailyReport', 'path': 'dailyReport', 'hidden': False, 'component': 'statistics/daily', 'meta': {'title': '收费日报表', 'icon': 'job', 'noCache': False, 'link': None}}, {'name': 'DailyFee', 'path': 'dailyFee', 'hidden': False, 'component': 'statistics/dailyFee', 'meta': {'title': '收费项汇总表', 'icon': 'excel', 'noCache': False, 'link': None}}, {'name': 'PayLog', 'path': 'payLog', 'hidden': False, 'component': 'statistics/paylog', 'meta': {'title': '收费明细表', 'icon': 'date-range', 'noCache': False, 'link': None}}, {'name': 'NextFee', 'path': 'nextFee', 'hidden': False, 'component': 'statistics/nextFee', 'meta': {'title': '待生成的费用', 'icon': 'skill', 'noCache': False, 'link': None}}, {'name': 'PreAccount', 'path': 'preAccount', 'hidden': False, 'component': 'statistics/preAccount', 'meta': {'title': '预收款余额', 'icon': 'druid', 'noCache': False, 'link': None}}, {'name': 'Fee', 'path': 'fee', 'hidden': False, 'component': 'statistics/payment', 'meta': {'title': '收费统计', 'icon': 'chart', 'noCache': False, 'link': None}}, {'name': 'UserFee', 'path': 'userFee', 'hidden': False, 'component': 'statistics/payment/house', 'meta': {'title': '收费统计(商铺)', 'icon': 'chart', 'noCache': False, 'link': None}}, {'name': 'Overdue', 'path': 'overdue', 'hidden': False, 'component': 'statistics/overdue', 'meta': {'title': '欠费数据', 'icon': 'server', 'noCache': False, 'link': None}}]}, {'name': 'System', 'path': '/system', 'hidden': False, 'redirect': 'noRedirect', 'component': 'Layout', 'alwaysShow': True, 'meta': {'title': '系统管理', 'icon': 'system', 'noCache': False, 'link': None}, 'children': [{'name': 'User', 'path': 'user', 'hidden': False, 'component': 'system/user/index', 'meta': {'title': '用户管理', 'icon': 'user', 'noCache': False, 'link': None}}, {'name': 'Role', 'path': 'role', 'hidden': False, 'component': 'system/role/index', 'meta': {'title': '角色管理', 'icon': 'peoples', 'noCache': False, 'link': None}}, {'name': 'Menu', 'path': 'menu', 'hidden': False, 'component': 'system/menu/index', 'meta': {'title': '菜单管理', 'icon': 'tree-table', 'noCache': False, 'link': None}}, {'name': 'Dept', 'path': 'dept', 'hidden': False, 'component': 'system/dept/index', 'meta': {'title': '部门管理', 'icon': 'tree', 'noCache': False, 'link': None}}, {'name': 'Post', 'path': 'post', 'hidden': False, 'component': 'system/post/index', 'meta': {'title': '岗位管理', 'icon': 'post', 'noCache': False, 'link': None}}, {'name': 'Dict', 'path': 'dict', 'hidden': False, 'component': 'system/dict/index', 'meta': {'title': '字典管理', 'icon': 'dict', 'noCache': False, 'link': None}}, {'name': 'Config', 'path': 'config', 'hidden': False, 'component': 'system/config/index', 'meta': {'title': '参数设置', 'icon': 'edit', 'noCache': False, 'link': None}}, {'name': 'Notice', 'path': 'notice', 'hidden': False, 'component': 'system/notice/index', 'meta': {'title': '通知公告', 'icon': 'message', 'noCache': False, 'link': None}}, {'name': 'Log', 'path': 'log', 'hidden': False, 'redirect': 'noRedirect', 'component': 'ParentView', 'alwaysShow': True, 'meta': {'title': '日志管理', 'icon': 'log', 'noCache': False, 'link': None}, 'children': [{'name': 'Operlog', 'path': 'operlog', 'hidden': False, 'component': 'monitor/operlog/index', 'meta': {'title': '操作日志', 'icon': 'form', 'noCache': False, 'link': None}}, {'name': 'Logininfor', 'path': 'logininfor', 'hidden': False, 'component': 'monitor/logininfor/index', 'meta': {'title': '登录日志', 'icon': 'logininfor', 'noCache': False, 'link': None}}]}]}, {'name': 'Monitor', 'path': '/monitor', 'hidden': False, 'redirect': 'noRedirect', 'component': 'Layout', 'alwaysShow': True, 'meta': {'title': '系统监控', 'icon': 'monitor', 'noCache': False, 'link': None}, 'children': [{'name': 'Online', 'path': 'online', 'hidden': False, 'component': 'monitor/online/index', 'meta': {'title': '在线用户', 'icon': 'online', 'noCache': False, 'link': None}}, {'name': 'Job', 'path': 'job', 'hidden': False, 'component': 'monitor/job/index', 'meta': {'title': '定时任务', 'icon': 'job', 'noCache': False, 'link': None}}, {'name': 'Druid', 'path': 'druid', 'hidden': False, 'component': 'monitor/druid/index', 'meta': {'title': '数据监控', 'icon': 'druid', 'noCache': False, 'link': None}}, {'name': 'Server', 'path': 'server', 'hidden': False, 'component': 'monitor/server/index', 'meta': {'title': '服务监控', 'icon': 'server', 'noCache': False, 'link': None}}, {'name': 'Cache', 'path': 'cache', 'hidden': False, 'component': 'monitor/cache/index', 'meta': {'title': '缓存监控', 'icon': 'redis', 'noCache': False, 'link': None}}]}, {'name': 'Tool', 'path': '/tool', 'hidden': False, 'redirect': 'noRedirect', 'component': 'Layout', 'alwaysShow': True, 'meta': {'title': '系统工具', 'icon': 'tool', 'noCache': False, 'link': None}, 'children': [{'name': 'Build', 'path': 'build', 'hidden': False, 'component': 'tool/build/index', 'meta': {'title': '表单构建', 'icon': 'build', 'noCache': False, 'link': None}}, {'name': 'Gen', 'path': 'gen', 'hidden': False, 'component': 'tool/gen/index', 'meta': {'title': '代码生成', 'icon': 'code', 'noCache': False, 'link': None}}, {'name': 'Swagger', 'path': 'swagger', 'hidden': False, 'component': 'tool/swagger/index', 'meta': {'title': '系统接口', 'icon': 'swagger', 'noCache': False, 'link': None}}]}]}
# 提取父节点的名称
# 提取父节点的名称
parentmenu=response['data'][0]['meta']['title']
print(parentmenu)

输出结果:

物业收费
# 提取子节点的名称
# 提取子节点的名称
childmenu=response['data'][0]['children'][0]['meta']['title']
print(childmenu)

输出结果:

商铺综合收费
#子节点总个数问题
#子节点总个数问题
childmenu=response['data'][2]['children']
childcount=len(childmenu)
print(childcount)

输出结果:

4

3、for循环获取对应的父子节点的名称和父子节点的总数试验V2

首先获取父节点的名字:

# 获取父节点的名称
for i in range(0,7):
    parentmenu=response['data'][i]['meta']['title']
    print(parentmenu)

可以看到,返回值data下的列表第一个字典的meta下面,对应title就是我们要取到的父节点的名字。

输出结果:

物业收费
收费数据管理
基础数据管理
统计报表
系统管理
系统监控
系统工具

接着提取子节点的名称,

# 获取子节点的名称
for j in range(0,3):
    childmenu=response['data'][0]['children'][j]['meta']['title']
    print(childmenu)

输出结果:

商铺综合收费
临时收费
押金管理

获取所有子节点的个数:

#获取所有子节点的个数
for k in range(0,7):
    childmenu=response['data'][k]['children']
    childcount=len(childmenu)
    print(childcount)

输出结果:

4
2
4
8
9
5
3

此时可以用try ,except改进获取所有子节点的名称

# 获取所有子节点的名称
for m in range(0,7):
    print(m + 1)
    for j in range(0,9):
        try:
            childmenu=response['data'][m]['children'][j]['meta']['title']
        except:
            break
        else:
            print(childmenu)

这里m+1是用来展示序号的。

1
商铺综合收费
临时收费
押金管理
预存款管理
2
抄表数据管理
商铺收费数据
3
收费项管理
商业区管理
商铺管理
商铺租售
4
收费日报表
收费项汇总表
收费明细表
待生成的费用
预收款余额
收费统计
收费统计(商铺)
欠费数据
5
用户管理
角色管理
菜单管理
部门管理
岗位管理
字典管理
参数设置
通知公告
日志管理
6
在线用户
定时任务
数据监控
服务监控
缓存监控
7
表单构建
代码生成
系统接口

4、列表获取对应的父子节点的名称和父子节点的总数试验V3

1、提取父子节点的内容存入list中
parentList=[]
for i in range(0,7):
    parentmenu=response['data'][i]['meta']['title']
    print(parentmenu)
    parentList.append(parentmenu)

parentnum=len(parentList)
print(parentnum)

其中parentmenu中存放的是父节点的列表,parentnum打印的是父节点的个数。

输出结果:

物业收费
收费数据管理
基础数据管理
统计报表
系统管理
系统监控
系统工具
7
2、提取子节点的内容存入list中
childList=[]
for m in range(0,7):
    print(m+1)
    childtmp = []
    for j in range(0,9):
        try:
            childmenu=response['data'][m]['children'][j]['meta']['title']
        except:
            break
        else:
            print(childmenu)
            childtmp.append(childmenu)
    print(childtmp)
    childList.append(childtmp)
print(childList)

这里注意,

print(m+1)   #打印父节点的顺序
print(childtmp)   #打印子节点的临时列表
print(childList)  #最后将整个列表内容输出
1
商铺综合收费
临时收费
押金管理
预存款管理
['商铺综合收费', '临时收费', '押金管理', '预存款管理']
2
抄表数据管理
商铺收费数据
['抄表数据管理', '商铺收费数据']
3
收费项管理
商业区管理
商铺管理
商铺租售
['收费项管理', '商业区管理', '商铺管理', '商铺租售']
4
收费日报表
收费项汇总表
收费明细表
待生成的费用
预收款余额
收费统计
收费统计(商铺)
欠费数据
['收费日报表', '收费项汇总表', '收费明细表', '待生成的费用', '预收款余额', '收费统计', '收费统计(商铺)', '欠费数据']
5
用户管理
角色管理
菜单管理
部门管理
岗位管理
字典管理
参数设置
通知公告
日志管理
['用户管理', '角色管理', '菜单管理', '部门管理', '岗位管理', '字典管理', '参数设置', '通知公告', '日志管理']
6
在线用户
定时任务
数据监控
服务监控
缓存监控
['在线用户', '定时任务', '数据监控', '服务监控', '缓存监控']
7
表单构建
代码生成
系统接口
['表单构建', '代码生成', '系统接口']
[['商铺综合收费', '临时收费', '押金管理', '预存款管理'], ['抄表数据管理', '商铺收费数据'], ['收费项管理', '商业区管理', '商铺管理', '商铺租售'], ['收费日报表', '收费项汇总表', '收费明细表', '待生成的费用', '预收款余额', '收费统计', '收费统计(商铺)', '欠费数据'], ['用户管理', '角色管理', '菜单管理', '部门管理', '岗位管理', '字典管理', '参数设置', '通知公告', '日志管理'], ['在线用户', '定时任务', '数据监控', '服务监控', '缓存监控'], ['表单构建', '代码生成', '系统接口']]

Process finished with exit code 0

愿每个测试都能顺利转为测试开发,提高职业技能,成为前1%的存在,为社会创造更大的价值,为公司节约更多的成本,为自己和家庭谋求更高的收入,所有人不受职业年龄限制,越老越吃香,直至财富自由;愿测试技术越来越进步,软件质量进一步得到提高,效率提高。愿祖国更加美好,人民更加幸福。多喜乐,常安宁。

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值