Python接口查询物流
快递鸟注册链接:http://www.kdniao.com/UserCenter/v4/UserHome.aspx
免费版是6个月,一天500条 免费版本只能查四个快递公司,公司和对应的编号 [(‘百世快递’, ‘HTKY’)(‘申通快递’,‘STO’), (‘圆通快递’,‘YTO’),(‘天天快递’,‘HHTT’)]
其他需要可以查看官方文档:http://www.kdniao.com/file/%E5%BF%AB%E9%80%92%E9%B8%9F%E6%8E%A5%E5%8F%A3%E6%8A%80%E6%9C%AF%E6%96%87%E6%A1%A3v5.42.pdf
api_key = ‘’ # 需要注册完获取 user_id = ‘’ # 需要注册完获取 在我的会员可以查看到
view.py(这部分都差不多)
def get_logistics_info(number, code):
def get_dataSign(req_data, appKey):
md5 = hashlib.md5()
md5.update(req_data.encode('utf-8'))
md5.update(appKey.encode('utf-8'))
return base64.b64encode(md5.hexdigest().encode('utf-8'))
api_key = '' # 需要注册完获取
user_id = '' # 需要注册完获取
order_num = number # 运单编号
ShipperCode = code # 快递公司编码
url = 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx'
order_data = {
"ShipperCode": ShipperCode,
"LogisticCode": order_num,
}
data = {
"RequestData": json.dumps(order_data).encode('utf-8'),
"EBusinessID": user_id,
"RequestType": 1002,
"DataSign": get_dataSign(json.dumps(order_data), api_key),
}
headers = {
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36",
}
response = requests.post(url=url, data=data, headers=headers)
return response.content.decode('utf-8')
view.py(调用函数)用于传入快递公司和单号
@csrf_exempt
def Search_orders(request):
if request.method == 'POST':
//ajax传入的值
Logistics = logistics.objects.get(Order_id_id=order_id)
company = Logistics.Company //快递公司
tra_number = Logistics.Tracking_number
ret = get_logistics_info(tra_number, company)
//直接传值
ret = get_logistics_info(‘快递单号’, ’快递公司‘)
print(ret)
return JsonResponse(json.dumps(ret),safe=False)
最后查询结果:
{
"LogisticCode" : "",
"ShipperCode" : "STO",
"Traces" : [ {
"AcceptStation" : "【浙江诸暨公司】的收件员【自动化1(0575-80701333)】已收件",
"AcceptTime" : "2021-12-15 23:23:16"
}, {
"AcceptStation" : "快件由【浙江诸暨公司】发往【浙江金华转运中心】",
"AcceptTime" : "2021-12-15 23:35:55"
} ],
"State" : "2",
"EBusinessID" : "",
"Success" : true
}
|