直接上代码
#!/usr/bin/env python3
# _*_ coding: utf-8 _*_
# File : kd100.py
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
# Date : 2019/7/29
import requests
import json
class Express100:
def __init__(self,code):
self.code = code
company_url = "http://www.kuaidi100.com/autonumber/autoComNum"
trace_url = "http://www.kuaidi100.com/query"
@classmethod
def get_json_data(self, url, payload):
r = requests.get(url=url, params=payload)
return r.json()
@classmethod
def get_company_info(self, express_code):
"""
{
comCode: "",
num: "3351419285305",
auto: [
{
comCode: "shentong",
id: "",
noCount: 13852,
noPre: "33514",
startTime: ""
}
]
}
"""
payload = {'text': express_code}
data = self.get_json_data(self.company_url, payload)
return data
@classmethod
def get_express_info(self, express_code):
"""
{
message: "ok",
nu: "3351419285305",
ischeck: "0",
condition: "00",
com: "shentong",
status: "200",
state: "0",
data: [
{
time: "2018-01-21 22:19:45",
ftime: "2018-01-21 22:19:45",
context: "淄博市 山东淄博公司-已发往-辽宁盘锦中转部",
location: ""
},
]
}
"""
company_info = self.get_company_info(express_code)
company_code = ""
if company_info.get("auto", ""):
company_code = company_info.get("auto", "")[0].get("comCode", "")
payload = {'type': company_code, 'postid': express_code, 'id': 1}
data = self.get_json_data(self.trace_url, payload)
data.update(company_info)
return data
def get_info(self):
ret = self.get_express_info(self.code)
ret = json.dumps(ret, ensure_ascii=False, sort_keys=True, indent=4)
return ret
if __name__ == "__main__":
a = Express100("75160097762655")
print(a.get_info())