import json import requests import time from urllib import request import sys def callurllab(): ip = input("cms-ipaddress:") urllab(ip) def urllab(ip): url="http://{}:2015/api/ListStream".format(ip) try: resp = request.urlopen(url) request.urlretrieve(url,ip+'.json') # 用‘urllib’模块下载数据到本地为ret.json,里面 print("下载数据中......") time.sleep(3) print("获取数据成功!") except Exception as e: print("获取数据失败") def calanylistdata(): jsonpath=input("input jsonfilepath:") anylistdata(jsonpath) def anylistdata(jsonpath): with open(jsonpath,"r",encoding='utf-8')as fp: data=json.load(fp) # print(data) # print(type(data)) #dic字典类型 arraydata=data["ListStream"]["lives"] #列表数据 # print(arrydata) # print(type(arrydata)) for dicitem in arraydata: print(dicitem["channel"]["name"],"\t",dicitem["channel"]["id"],"\t",dicitem["client"]["host"]) def quit(): sys.exit() func_dic = { '1': callurllab, '2': calanylistdata, '8':quit } if __name__ == '__main__': while True: print(''' ===程序运行中保持专网的连通=== ===========功能列表=========== 1.下载数据 2.数据分析 3.退出系统 ===========auther======= ''') choice = input('请输入功能编号:').strip() if choice not in func_dic: print("输入正确的编号:") continue func_dic.get(choice)()
重要代码:
一.下载代码重要片段:
from urllib import request
resp = request.urlopen(url) request.urlretrieve(url,ip+'.json') # 用‘urllib’模块下载数据到本地为ret.json,里面
上述代码,把文件下载在本地‘ip.json’文件
二.读取代码,读取代码结合下载的接送数据内容进行分析,进行打印
with open(jsonpath,"r",encoding='utf-8')as fp: data=json.load(fp) # print(data) # print(type(data)) #dic字典类型 arraydata=data["ListStream"]["lives"] #列表数据 # print(arrydata) # print(type(arrydata)) for dicitem in arraydata: print(dicitem["channel"]["name"],"\t",dicitem["channel"]["id"],"\t",dicitem["client"]["host"])