通过curl获取接口内容,以及json内容
接口内容解析:
Json内容解析
使用方式
将代码保存到streamlit项目page目录下即可
代码
import streamlit as st
# 使用curl2py需要pip install filestools
from curl2py.curlParseTool import parse_curl_cmd as pcc
import json
import requests
st.set_page_config(
page_title="接口数据解析",
page_icon="🧗",
layout="wide"
)
# 查询返回内容深度
depth=1
def spider(curl):
# 根据curl请求数据,返回json
context=pcc(curl)
if context['method'] =='get':
res = requests.get( url = context['url'],
headers = context['headers'],
cookies = context['cookies'],
params = context['params'],
data = context['data'],
)
elif context['method'] =='post':
res = requests.post( url = context['url'],
headers = context['headers'],
cookies = context['cookies'],
params = context['params'],
data = context['data'],
)
return json.loads(res.text)
def display(dic_list):
# 递归查询json数据的内容,并分层展示出来
global depth
for dic in dic_list :
keys = dic.keys()
show_data =[] # 展示的json内容
show_list = [] # 展示的list内容
next_dic = [] # 下级查询内容
for key in keys:
if isinstance(dic[key],dict):
next_dic.append(dic[key])
if isinstance(dic[key],list):
show_list += dic[key]
show_data.append([key,dic[key]])
st.success(f'第{depth}层')
st.dataframe( show_data)
if len(show_list)>0:
st.warning("列表数据:")
st.dataframe(show_list)
if len(next_dic)>0:
depth=depth+1
display(next_dic)
# 刷新页面保存curl
if 'curl_text' not in st.session_state:
st.session_state['curl_text'] = ''
with st.expander( "接口内容解析",expanded=False):
curl_text = st.text_area("输入curl:",value=st.session_state['curl_text'])
if st.button('查询',key=1):
if len(curl_text)>0:
st.session_state['curl_text'] = curl_text
res = spider(curl_text)
#st.write(res)
display([res])
with st.expander( "json内容解析",expanded=False):
json_text = st.text_area("输入json:")
if st.button('查询',key=2):
if len(json_text)>0:
res = json.loads(json_text)
#st.write(res)
display([res])
评论区