python|调用快递鸟API查询物流

首先在快递鸟申请一个账号:http://www.kdniao.com/ 快递鸟的账号是申请了就能用,每天免费查询3000次
python|调用快递鸟API查询物流
文章图片

然后在我的会员中心查看自己的用户ID 和 API key
然后在及时查询中可以找到对接文档和一些实例,我下面写的python对接的实例
python|调用快递鸟API查询物流
文章图片


import json import urllib import urllib.request import hashlib import base64 import urllib.parse# 此处为快递鸟官网申请的帐号和密码 APP_id = "1342342"#更换成自己的id APP_key = "3edf3-a6sd16-43sdf5c-bsd817-6a85af8e64b5"#更换成自己的keydef encrypt(logistic_code, app_key): """数据内容签名:把(请求内容(未编码)+AppKey)进行MD5加密,然后Base64编码""" m = hashlib.md5() m.update((logistic_code+app_key).encode("utf8")) encode_sign = m.hexdigest() data_sign = base64.b64encode(encode_sign.encode(encoding='utf-8')) return data_sign def send_post(url, data): """发送post请求""" post_data = https://www.it610.com/article/urllib.parse.urlencode(data).encode('utf-8') # 设置请求头 header = { "Accept": "application/x-www-form-urlencoded; charset=utf-8", "Accept-Encoding": "utf-8" } req = urllib.request.Request(url, post_data, header) get_data = https://www.it610.com/article/(urllib.request.urlopen(req).read().decode('utf-8')) return get_datadef get_company(logistic_code, app_id, app_key, url): """获取对应快递单号的快递公司代码和名称""" data1 = {'LogisticCode': logistic_code} d1 = json.dumps(data1, sort_keys=True) data_sign = encrypt(d1, app_key)post_data = https://www.it610.com/article/{'RequestData': d1, 'EBusinessID': app_id, 'RequestType': '2002',# 单号识别接口编码 'DataType': '2', 'DataSign': data_sign.decode() }json_data = https://www.it610.com/article/send_post(url, post_data) sort_data = json.loads(json_data) return sort_datadef get_traces(logistic_code, shipper_code, app_id, app_key, url):"""查询接口支持按照运单号查询(单个查询)""" data1 = {'LogisticCode': logistic_code, 'ShipperCode': shipper_code} d1 = json.dumps(data1, sort_keys=True) data_sign = encrypt(d1, app_key)post_data = https://www.it610.com/article/{'RequestData': d1, 'EBusinessID': app_id, 'RequestType': '1002',# 即时查询接口编码 'DataType': '2', 'DataSign': data_sign.decode() } json_data = https://www.it610.com/article/send_post(url, post_data) sort_data = json.loads(json_data) return sort_datadef recognise(express_code):"""输出数据""" url = 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx' data = https://www.it610.com/article/get_company(express_code, APP_id, APP_key, url) print(data) if not data['Shippers']: print("未查到该快递信息,请检查快递单号是否有误!") else: trace_data = https://www.it610.com/article/get_traces(express_code, data['Shippers'][0]['ShipperCode'], APP_id, APP_key, url) print(trace_data) if trace_data['Success'] == "false" or not trace_data['Traces']: print("未查询到该快递物流轨迹!") else: str_state = "无轨迹" if trace_data['State'] == '1': str_state = '已揽收' if trace_data['State'] == '2': str_state = "在途中" if trace_data['State'] == '3': str_state = "已签收" print("目前状态: "+str_state) trace_data = https://www.it610.com/article/trace_data['Traces']for item in trace_data: print(str(trace_data.index(item))+":", item['AcceptTime'], item['AcceptStation'])print("\n") returncode = 701934534553453#快递单号 if __name__ == '__main__':recognise(code)

【python|调用快递鸟API查询物流】

    推荐阅读