python3快递100查信息

直接上代码

#!/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())

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值