V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
justincnn
V2EX  ›  Python

Python 求助之快递鸟快递查询

  •  
  •   justincnn · 2020-02-23 16:53:35 +08:00 · 2656 次点击
    这是一个创建于 1523 天前的主题,其中的信息可能已经有所发展或是发生改变。
    下面是网上找的一段快递鸟的查询代码,先根据快递单号来判断属于哪个快递公司,然后再查询,
    如果直接指定快递公司再查询,请问这个如何操作啊?

    下面是网上找的一段快递鸟的查询代码,先根据快递单号来判断属于哪个快递公司,然后再查询,
    如果直接指定快递公司再查询,请问这个如何操作啊?


    # !/usr/bin/python
    # encoding:utf-8

    import json
    import urllib
    import urllib.request
    import hashlib
    import base64
    import urllib.parse

    # 此处为快递鸟官网申请的帐号和密码
    APP_id = "1266271"
    APP_key = "7526a46e-3a2a-4f5b-8659-d72f361e3386"


    def encrypt(origin_data, appkey):
    """数据内容签名:把(请求内容(未编码)+AppKey)进行 MD5 加密,然后 Base64 编码"""
    m = hashlib.md5()
    m.update((origin_data+appkey).encode("utf8"))
    encodestr = m.hexdigest()
    base64_text = base64.b64encode(encodestr.encode(encoding='utf-8'))
    return base64_text


    def sendpost(url, datas):
    """发送 post 请求"""
    postdata = urllib.parse.urlencode(datas).encode('utf-8')
    header = {
    "Accept": "application/x-www-form-urlencoded;charset=utf-8",
    "Accept-Encoding": "utf-8"
    }
    req = urllib.request.Request(url, postdata, header)
    get_data = (urllib.request.urlopen(req).read().decode('utf-8'))
    return get_data


    def get_company(logistic_code, appid, appkey, url):
    """获取对应快递单号的快递公司代码和名称"""
    data1 = {'LogisticCode': logistic_code}
    d1 = json.dumps(data1, sort_keys=True)
    requestdata = encrypt(d1, appkey)
    post_data = {
    'RequestData': d1,
    'EBusinessID': appid,
    'RequestType': '2002',
    'DataType': '2',
    'DataSign': requestdata.decode()}
    json_data = sendpost(url, post_data)
    sort_data = json.loads(json_data)
    return sort_data


    def get_traces(logistic_code, shipper_code, appid, appkey, url):
    """查询接口支持按照运单号查询(单个查询)"""
    data1 = {'LogisticCode': logistic_code, 'ShipperCode': shipper_code}
    d1 = json.dumps(data1, sort_keys=True)
    requestdata = encrypt(d1, appkey)
    post_data = {'RequestData': d1, 'EBusinessID': appid, 'RequestType': '1002', 'DataType': '2',
    'DataSign': requestdata.decode()}
    json_data = sendpost(url, post_data)
    sort_data = json.loads(json_data)
    return sort_data


    def recognise(expresscode):
    """输出数据"""
    url = 'http://testapi.kdniao.cc:8081/Ebusiness/EbusinessOrderHandle.aspx'
    data = get_company(expresscode, APP_id, APP_key, url)
    if not any(data['Shippers']):
    print("未查到该快递信息,请检查快递单号是否有误!")
    else:
    print("已查到该", str(data['Shippers'][0]['ShipperName'])+"("+
    str(data['Shippers'][0]['ShipperCode'])+")", expresscode)
    trace_data = get_traces(expresscode, data['Shippers'][0]['ShipperCode'], APP_id, APP_key, url)
    if trace_data['Success'] == "false" or not any(trace_data['Traces']):
    print("未查询到该快递物流轨迹!")
    else:
    str_state = "问题件"
    if trace_data['State'] == '2':
    str_state = "在途中"
    if trace_data['State'] == '3':
    str_state = "已签收"
    print("目前状态: "+str_state)
    trace_data = trace_data['Traces']
    item_no = 1
    for item in trace_data:
    print(str(item_no)+":", item['AcceptTime'], item['AcceptStation'])
    item_no += 1
    print("\n")
    return

    while True:
    code = input("请输入快递单号(Esc 退出):")
    code = code.strip()
    if code == "esc":
    break
    recognise(code)
    6 条回复    2020-02-25 11:34:06 +08:00
    Perterually
        1
    Perterually  
       2020-02-23 18:54:42 +08:00
    pip install kd100
    crella
        2
    crella  
       2020-02-23 19:41:00 +08:00 via Android
    鉴于 py 代码排版比较特殊,建议你把代码发到 github、gitee、coding 或者 ubuntu paste,再粘贴过来。这样没法看缩进
    sleepm
        3
    sleepm  
       2020-02-24 13:32:34 +08:00
    http://www.kdniao.com/api-track
    传入 ShipperCode
    文档底下有 '查看快递公司编码' ctrl+f 页内查找
    自己试着封装下快递鸟的代码,有好处
    justincnn
        4
    justincnn  
    OP
       2020-02-25 11:33:41 +08:00
    @crella 已经解决了,谢谢
    justincnn
        5
    justincnn  
    OP
       2020-02-25 11:33:57 +08:00
    @Perterually 已经解决了,谢谢啊
    justincnn
        6
    justincnn  
    OP
       2020-02-25 11:34:06 +08:00
    @sleepm 已经解决了,谢谢啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4040 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 05:23 · PVG 13:23 · LAX 22:23 · JFK 01:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.