You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
3.9 KiB
127 lines
3.9 KiB
#!/usr/bin/env python3
|
|
"""
|
|
使用快递100查询物流信息
|
|
"""
|
|
|
|
import requests
|
|
import json
|
|
import sys
|
|
|
|
def query_kuaidi100(tracking_number):
|
|
"""
|
|
使用快递100查询物流信息
|
|
"""
|
|
try:
|
|
# 快递100的查询API
|
|
url = "https://www.kuaidi100.com/query"
|
|
|
|
# 自动识别快递公司
|
|
auto_url = f"https://www.kuaidi100.com/autonumber/auto?num={tracking_number}"
|
|
|
|
headers = {
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
|
'Referer': 'https://www.kuaidi100.com/',
|
|
'Accept': 'application/json, text/plain, */*',
|
|
}
|
|
|
|
# 首先自动识别快递公司
|
|
print("正在识别快递公司...")
|
|
auto_response = requests.get(auto_url, headers=headers, timeout=10)
|
|
|
|
if auto_response.status_code == 200:
|
|
auto_data = auto_response.json()
|
|
if auto_data:
|
|
# 选择第一个识别结果
|
|
company_code = auto_data[0].get('comCode', '')
|
|
company_name = auto_data[0].get('name', '')
|
|
|
|
if company_code:
|
|
print(f"识别到的快递公司: {company_name} ({company_code})")
|
|
|
|
# 查询物流详情
|
|
params = {
|
|
'type': company_code,
|
|
'postid': tracking_number,
|
|
'id': 1,
|
|
'valicode': '',
|
|
'temp': '0.12345678901234567'
|
|
}
|
|
|
|
response = requests.get(url, params=params, headers=headers, timeout=10)
|
|
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
return parse_kuaidi100_response(data, company_name)
|
|
else:
|
|
return f"查询失败,HTTP状态码: {response.status_code}"
|
|
else:
|
|
return "无法识别快递公司"
|
|
else:
|
|
return "无法识别快递公司"
|
|
else:
|
|
return f"识别快递公司失败,HTTP状态码: {auto_response.status_code}"
|
|
|
|
except Exception as e:
|
|
return f"查询过程中出现错误: {str(e)}"
|
|
|
|
def parse_kuaidi100_response(data, company_name):
|
|
"""
|
|
解析快递100的响应
|
|
"""
|
|
if not data:
|
|
return "未收到有效响应"
|
|
|
|
status = data.get('status', '')
|
|
message = data.get('message', '')
|
|
|
|
result = []
|
|
result.append(f"快递公司: {company_name}")
|
|
|
|
if status == '200':
|
|
result.append(f"查询状态: 成功")
|
|
|
|
# 快递状态
|
|
state = data.get('state', '')
|
|
state_desc = {
|
|
'0': '在途',
|
|
'1': '揽收',
|
|
'2': '疑难',
|
|
'3': '签收',
|
|
'4': '退签',
|
|
'5': '派件',
|
|
'6': '退回'
|
|
}.get(state, '未知状态')
|
|
|
|
result.append(f"物流状态: {state_desc}")
|
|
|
|
# 物流轨迹
|
|
traces = data.get('data', [])
|
|
if traces:
|
|
result.append("")
|
|
result.append("物流轨迹:")
|
|
for trace in traces:
|
|
time_str = trace.get('time', '')
|
|
desc = trace.get('context', '')
|
|
if time_str and desc:
|
|
result.append(f" {time_str} - {desc}")
|
|
else:
|
|
result.append("暂无物流轨迹信息")
|
|
else:
|
|
result.append(f"查询失败: {message}")
|
|
|
|
return "\n".join(result)
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print("使用方法: python kuaidi100_query.py <快递单号>")
|
|
sys.exit(1)
|
|
|
|
tracking_number = sys.argv[1]
|
|
print(f"正在查询快递单号: {tracking_number}")
|
|
print("=" * 50)
|
|
|
|
result = query_kuaidi100(tracking_number)
|
|
print(result)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|