来源:【万维易源】快递物流查询-单号自动识别-快递接口查询-快递单号查询-物流轨迹追踪-支持顺丰快递查询
介绍:
电商人必备,可以批量查询快递单号,基于万维易源提供的调用示例进行改动,其他的api不确保能用
2024-8-15更新日志
去除新建表格功能
新增更新物流时间
新增直接覆盖原表格文件保留原有样式大小
2024-11-4更新日志
因为部分单号需要用到手机号后四位所以新增了这个功能,现在中通也需要了,所以我就添加了
代码:
原代码
#需要安装: pip install requests
import requests
#以下需要修改
appcode="xxxxx"
req_data = {
'com': 'yuantong',
'nu': 'YT4620020577123',
'receiverPhone': '',
'senderPhone': ''
}
#修改结束
url="http://ali-deliver.showapi.com/showapi_expInfo"
headers = {
'Authorization':'APPCODE ' + appcode
}
try:
html = requests.get(url, headers=headers,data=req_data)
except :
print("URL错误")
exit()
print("---------response status is:-------------")
print(html.status_code )
print("---------response headers are:-------------")
print(html.headers)
msg = html.headers.get('X-Ca-Error-Message')
status=html.status_code
if status == 200 :
print("status为200,请求成功,计费1次。(status非200时都不计费)")
else:
if(status == 400 and msg == 'Invalid AppCode'):
print("AppCode不正确,请到用户后台获取正确的AppCode: https://market.console.aliyun.com/imageconsole/index.htm")
elif(status == 400 and msg == 'Invalid Path or Method'):
print("url地址或请求的'GET'|'POST'方式不对")
elif(status == 403 and msg == 'Unauthorized'):
print("服务未被授权,请检查是否购买")
elif(status == 403 and msg == 'Quota Exhausted'):
print("套餐资源包次数已用完")
elif(status == 500 ):
print("API网关错误")
else:
print("参数名错误或其他错误")
print(status)
print(msg)
print("---------response body is:-------------")
print(html.text)
改完代码
import tkinter as tk
from tkinter import filedialog, messagebox, Entry, Button, Label, Text, Scrollbar
import pandas as pd
import requests
from openpyxl import load_workbook
# 请求URL(请替换为有效的API URL)
url = "http://ali-deliver.showapi.com/showapi_expInfo"
# 初始化headers
headers = {}
# 默认快递公司代码
default_company_code = "zhongtong"
# 函数定义
def build_request_data(number):
company_code_value = company_code_entry.get().strip()
if not company_code_value:
company_code_value = default_company_code
sender_phone_value = sender_phone_entry.get().strip()
req_data = {
'com': company_code_value,
'nu': number
}
if sender_phone_value:
req_data['senderPhone'] = sender_phone_value
return req_data
def process_response(response):
try:
json_response = response.json()
if json_response.get('showapi_res_code') != 0:
return None, 'Error'
data = json_response.get('showapi_res_body', {}).get('data', [])
if not data:
return None, 'No Result'
latest_update = data[0].get('context', 'No Update Info')
update_time = data[0].get('time', 'Time Info Missing')
return latest_update, update_time
except Exception as e:
return None, str(e)
def save_config():
global appcode
appcode = appcode_entry.get().strip()
if appcode:
headers['Authorization'] = f'APPCODE {appcode}'
messagebox.showinfo("保存", "配置已保存")
else:
messagebox.showwarning("警告", "请填写AppCode")
def on_query_clicked():
input_path = filedialog.askopenfilename(
title="选择包含快递单号的Excel文件",
filetypes=[("Excel files", "*.xlsx")]
)
if not input_path:
return
column = column_entry.get().strip()
if not column:
messagebox.showerror("错误", "请提供列名")
return
try:
query_express_numbers(input_path, column)
except Exception as e:
messagebox.showerror("错误", f"查询过程中发生错误:{e}")
def query_express_numbers(input_path, column_name):
log_text.insert("end", "开始处理表格...\n")
log_text.see("end")
try:
book = load_workbook(filename=input_path)
sheet = book.active
df = pd.read_excel(input_path)
if column_name not in df.columns:
messagebox.showerror("错误", f"列名'{column_name}'不存在于表格中")
return
column_index = df.columns.get_loc(column_name) + 1
for row in sheet.iter_rows(min_row=2, max_col=sheet.max_column, max_row=sheet.max_row):
number = row[column_index - 1].value
if not number:
continue
last_update_cell = sheet.cell(row=row[0].row, column=2)
update_time_cell = sheet.cell(row=row[0].row, column=3)
if not (last_update_cell.value or update_time_cell.value):
log_text.insert("end", f"正在查询快递单号:{number}\n")
req_data = build_request_data(number)
response = requests.get(url, headers=headers, params=req_data)
response.raise_for_status()
context_value, update_time = process_response(response)
if context_value is None:
log_text.insert("end", "查询失败,已记录。\n")
else:
last_update_cell.value = context_value
update_time_cell.value = update_time
log_text.insert("end", f"单号 {number} 查询成功。\n")
book.save(filename=input_path)
log_text.insert("end", "保存完成。\n")
except Exception as e:
log_text.insert("end", f"保存失败:{e}\n")
# 创建主窗口
root = tk.Tk()
root.title("快递单号查询")
# 创建授权码AppCode输入框
appcode_label = Label(root, text="授权码AppCode:")
appcode_label.grid(row=0, column=0, padx=5, pady=5)
appcode_entry = Entry(root, width=50)
appcode_entry.grid(row=0, column=1, padx=5, pady=5)
# 创建快递公司代码输入框
company_code_label = Label(root, text="快递公司代码:")
company_code_label.grid(row=1, column=0, padx=5, pady=5)
company_code_entry = Entry(root, width=50)
company_code_entry.grid(row=1, column=1, padx=5, pady=5)
company_code_entry.insert(0, default_company_code)
# 创建发件人电话后四位输入框(非必填)
sender_phone_label = Label(root, text="发件人电话(可选):")
sender_phone_label.grid(row=2, column=0, padx=5, pady=5)
sender_phone_entry = Entry(root, width=30)
sender_phone_entry.grid(row=2, column=1, padx=5, pady=5)
# 创建快递单号列名输入框
column_label = Label(root, text="快递单号列名:")
column_label.grid(row=3, column=0, padx=5, pady=5)
column_entry = Entry(root, width=30)
column_entry.grid(row=3, column=1, padx=5, pady=5)
# 创建保存配置按钮
save_button = Button(root, text="保存配置", command=save_config)
save_button.grid(row=4, columnspan=2, sticky='nsew', padx=5, pady=5)
# 创建查询按钮
query_button = Button(root, text="查询快递单号", command=on_query_clicked)
query_button.grid(row=5, columnspan=2, sticky='nsew', padx=5, pady=5)
# 创建日志显示框
log_label = Label(root, text="处理信息:")
log_label.grid(row=6, columnspan=2, sticky='nsew', padx=5, pady=5)
log_text = Text(root, height=10, width=50, wrap='word')
log_text.grid(row=7, columnspan=2, sticky='nsew')
scrollbar = Scrollbar(root, command=log_text.yview)
scrollbar.grid(row=7, column=2, sticky='ns')
log_text.config(yscrollcommand=scrollbar.set)
# 运行主循环
root.mainloop()
功能:
添加了gui界面把所有操作都便捷化,不方便部署python环境的我会把应用打包出来放在底部
演示:
下载地址: