侧边栏壁纸
  • 累计撰写 18 篇文章
  • 累计创建 25 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

Streamlit解析接口内容和json内容

遗失的记忆
2024-08-23 / 1 评论 / 0 点赞 / 41 阅读 / 3258 字

通过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])

0

评论区