您的位置:首页 > 汽车 > 时评 > seo百度排名优化_网站升级中_bt种子bt天堂_竞价推广账户托管费用

seo百度排名优化_网站升级中_bt种子bt天堂_竞价推广账户托管费用

2025/4/20 23:24:39 来源:https://blog.csdn.net/xiaolipanpan/article/details/146531168  浏览:    关键词:seo百度排名优化_网站升级中_bt种子bt天堂_竞价推广账户托管费用
seo百度排名优化_网站升级中_bt种子bt天堂_竞价推广账户托管费用

需求

  1. 调用get label 接口
  2. 将接口返回的base64文件转换成pdf
  3. 文件命名用接口返回值的单号命名保存再指定的文件夹重

实现代码

# -*- coding: utf-8 -*-
import requests
import base64
import os
import json  # 新增json模块导入url = "http://releasud.com/api/label/LableApi/GalProcess"  # 替换为实际API地址
headers = {"Content-Type": "application/json","token": "MjAxNjA4MDkxBRDJBOThFNjVCMDBDNTc2OURCM0U3NA=="
}
body = {"TrackNumber": "F7532425032000IA","IsNeedLable": True  # 注意字段名与文档一致(原需求可能有拼写错误)
}try:# 发送请求response = requests.post(url, json=body, headers=headers)response.raise_for_status()  # 检查HTTP状态码response_data = response.json()# 调试:打印完整响应print("完整响应内容:\n", json.dumps(response_data, indent=2, ensure_ascii=False))# 检查关键字段结构if "Data" not in response_data:print("错误:响应中缺少Data字段")exit()data = response_data["Data"]label_encoded = data.get("Label")channel_trace_id = data.get("ChannelTraceId")msg = data.get("Msg", "无附加信息")# 检查Label是否为nullif label_encoded is None:print(f"错误:标签未生成,原因 -> {msg}")exit()if channel_trace_id is None:print("错误:ChannelTraceId为空")exit()# 解码并保存try:label_pdf = base64.b64decode(label_encoded)except Exception as e:print(f"Base64解码失败: {e}")exit()os.makedirs("Label", exist_ok=True)file_path = os.path.join("Label", f"{channel_trace_id}.pdf")with open(file_path, "wb") as f:f.write(label_pdf)print(f"文件保存成功: {file_path}")except ValueError as e:  # 兼容所有JSON解析错误(新版requests抛JSONDecodeError,旧版抛ValueError)print(f"错误:响应不是有效的JSON格式 -> {e}")print("原始响应内容:", response.text)
except requests.exceptions.RequestException as e:print(f"请求失败: {e}")
except KeyError as e:print(f"响应字段缺失: {e}")
except Exception as e:print(f"未知错误: {e}")

运行结果:

D:\PYTHON-学习\邮政接口压力测试脚本\pythonProject1\.venv\Scripts\python.exe D:\PYTHON-学习\邮政接口压力测试脚本\pythonProject1\huoqu_Label.py 
完整响应内容:{"Status": 1,"Data": {"Label": null,"Msg": "F7532425032000IA未查询到单号信息!","ChannelTraceId": null,"ServiceProviderName": null},"ErrCode": null,"ErrMsg": null
}
错误:标签未生成,原因 -> F7532425032000IA未查询到单号信息!进程已结束,退出代码为 0

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com