How to Query Chinese Courier Tracking Info with Python and Kuaidi100 API
This tutorial shows how to use Python's urllib and json libraries to call the Kuaidi100 API, retrieve real‑time logistics data for various Chinese courier companies, and display the tracking timeline, while explaining how to discover the correct request URL via browser dev tools.
Introduction
The article teaches how to use Python to query logistics information from the Kuaidi100 service.
Project Goal
Teach readers to retrieve their own courier tracking data using Python.
Preparation
Software: PyCharm. Required libraries: urllib.request, json. Target website: https://www.kuaidi100.com
Analyzing the API
Use Chrome DevTools to inspect a request for a sample tracking number, locate the request URL, for example:
https://www.kuaidi100.com/query?type=shentong&postid=773036432685909&temp=0.03191355265516216In the URL, postid is the tracking number and type is the company code.
Implementation
Define a class, a dictionary of company codes, and a loop to get user input, then request and parse the JSON response.
import urllib.request
import json
kd_dict = {1: 'shentong', 2: 'youzhengguonei', 3: 'yuantong', 4: 'shunfeng', 5: 'yunda', 6: 'zhongtong'}
class YU(object):
pass
while True:
print("输入要查询快递公司:")
print("1.申通 2.EMS邮政 3.圆通 4.顺风 5.韵达 6.中通 0.退出")
choose = int(input("请选择您的快递公司:"))
if choose == 0:
break
kd_num = input("请输入快递单号:")
url = "http://www.kuaidi100.com/query?type=%s&postid=%s" % (kd_dict[choose], kd_num)
response = urllib.request.urlopen(url)
html = response.read().decode('utf-8')
target = json.loads(html)
if target['status'] == '200':
data = target['data']
for item in data:
print("
时间: " + item['time'])
print("状态: " + item['context'])
print("
感谢使用!")
else:
print("输入有误请重新输入!")Result
Running the script displays the tracking timeline for the entered number, as shown in the screenshots.
Conclusion
Do not scrape massive amounts of data to avoid overloading the server.
The script demonstrates string concatenation and type conversion in Python.
It provides a clear view of logistics information.
Hands‑on practice is essential for deeper understanding.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
