调用快递鸟单号查询接口Api-查快递

38次阅读

共计 2541 个字符,预计需要花费 7 分钟才能阅读完成。

主要实现功能:根据用户输入的订单号,自动识别根据快递鸟查询快递 API 接口,实现自动查询的功能。

对接流程

1. 申请一个快递鸟的帐号获取授权申请 http://www.kdniao.com/reg

快递鸟的帐号使用永久免费的,且申请了进行实名认证立即就能用的,也不需要审核。

2. 后台选择需要开通的服务,按照单号识别接口和即时查询接口封装好数据

3. 获取返回的 json 数据输出

import json
import urllib
import urllib.request
import hashlib
import base64
import urllib.parse
 
# 此处为快递鸟官网申请的帐号和密码
APP_id = "1266271"
APP_key = "7526a46e-3a2a-4f5b-8659-d72f361e3386"
 
 
def encrypt(origin_data, appkey):
    """数据内容签名:把 (请求内容(未编码)+AppKey) 进行 MD5 加密,然后 Base64 编码"""
    m = hashlib.md5()
    m.update((origin_data+appkey).encode("utf8"))
    encodestr = m.hexdigest()
    base64_text = base64.b64encode(encodestr.encode(encoding='utf-8'))
    return base64_text
 
 
def sendpost(url, datas):
    """发送 post 请求"""
    postdata = urllib.parse.urlencode(datas).encode('utf-8')
    header = {
        "Accept": "application/x-www-form-urlencoded;charset=utf-8",
        "Accept-Encoding": "utf-8"
    }
    req = urllib.request.Request(url, postdata, header)
    get_data = (urllib.request.urlopen(req).read().decode('utf-8'))
    return get_data
 
 
def get_company(logistic_code, appid, appkey, url):
    """获取对应快递单号的快递公司代码和名称"""
    data1 = {'LogisticCode': logistic_code}
    d1 = json.dumps(data1, sort_keys=True)
    requestdata = encrypt(d1, appkey)
    post_data = {
        'RequestData': d1,
        'EBusinessID': appid,
        'RequestType': '2002',
        'DataType': '2',
        'DataSign': requestdata.decode()}
    json_data = sendpost(url, post_data)
    sort_data = json.loads(json_data)
    return sort_data
 
 
def get_traces(logistic_code, shipper_code, appid, appkey, url):
    """查询接口支持按照运单号查询(单个查询)"""
    data1 = {'LogisticCode': logistic_code, 'ShipperCode': shipper_code}
    d1 = json.dumps(data1, sort_keys=True)
    requestdata = encrypt(d1, appkey)
    post_data = {'RequestData': d1, 'EBusinessID': appid, 'RequestType': '1002', 'DataType': '2',
                 'DataSign': requestdata.decode()}
    json_data = sendpost(url, post_data)
    sort_data = json.loads(json_data)
    return sort_data
 
 
def recognise(expresscode):
    """输出数据"""
    url = 'http://testapi.kdniao.cc:8081/Ebusiness/EbusinessOrderHandle.aspx'
    data = get_company(expresscode, APP_id, APP_key, url)
    if not any(data['Shippers']):
        print("未查到该快递信息, 请检查快递单号是否有误!")
    else:
        print("已查到该", str(data['Shippers'][0]['ShipperName'])+"("+
              str(data['Shippers'][0]['ShipperCode'])+")", expresscode)
        trace_data = get_traces(expresscode, data['Shippers'][0]['ShipperCode'], APP_id, APP_key, url)
        if trace_data['Success'] == "false" or not any(trace_data['Traces']):
            print("未查询到该快递物流轨迹!")
        else:
            str_state = "问题件"
            if trace_data['State'] == '2':
                str_state = "在途中"
            if trace_data['State'] == '3':
                str_state = "已签收"
            print("目前状态:"+str_state)
            trace_data = trace_data['Traces']
            item_no = 1
            for item in trace_data:
                print(str(item_no)+":", item['AcceptTime'], item['AcceptStation'])
                item_no += 1
            print("\n")
    return
 
while True:
    code = input("请输入快递单号(Esc 退出):")
    code = code.strip()
    if code == "esc":
        break
    recognise(code)

正文完
 0