python解析网页上的json数据落地到EXCEL

安装必要的库

import requests
import pandas as pd
import os
import sys
import io
import urllib3
import json

测试数据

  • 网页上的数据结构如下
{
    "success": true,
    "code": "CIFM_0000",
    "encode": null,
    "message": "ok",
    "url": null,
    "total": 3,
    "items": [
        {
            "summaryDate": "20240611",
            "summaryType": "naturalDay",
            "workday": true,
            "newCustNum": 1,
            "haveCustNum": 1691627,
            "newAccountNum": 2,
            "haveAccountNum": 1692934,
            "totalShare": 4947657341.69,
            "netCash": -3523387.25,
            "yield": 0.01386
        },
        {
            "summaryDate": "20240612",
            "summaryType": "naturalDay",
            "workday": true,
            "newCustNum": 5,
            "haveCustNum": 1672766,
            "newAccountNum": 5,
            "haveAccountNum": 1674071,
            "totalShare": 4927109080.29,
            "netCash": -20735233.55,
            "yield": 0.01387
        },
        {
            "summaryDate": "20240613",
            "summaryType": "naturalDay",
            "workday": true,
            "newCustNum": 4,
            "haveCustNum": 1662839,
            "newAccountNum": 5,
            "haveAccountNum": 1664146,
            "totalShare": 4927405885.59,
            "netCash": 110659.8,
            "yield": 0.01389
        }
    ],
    "data": null,
    "info": null
}

详细逻辑代码

import requests
import pandas as pd
import os
import sys
import io
import urllib3
import json

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

url = "https://ip/ma/web/trade/dailySummary?startDate={pi_startdate}&endDate={pi_enddate}"
headers = {
   
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0",
}

def save_data(data, columns, excel_path, sheet_name):
    df = pd.DataFrame(data, columns=columns)
    if not os.path.exists(excel_path):
        df.to_excel(excel_path, sheet_name=sheet_name, index=False)
    else:
        with pd.ExcelWriter(excel_path, engine='openpyxl', mode=&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脸ル粉嘟嘟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值