python常用脚本查询快递

边学边练。用python查询快递。

# coding:utf-8
'''
Created on 2018年3月16日下午12:12:52}

@author: chw
'''

import requests as req 
import json
import sys

def getExpress(company, code):
    '''funcation: Get Express Infomation
    2 args
    company: the express company code
    code: the express bill code
    '''
    url = 'http://www.kuaidi100.com/query?type={}&postid={}'.format(company, code)
    
    response = req.get(url)
    if response.status_code != 200:
        print ('查询失败!{}'.format(response.status_code))
        return None
    
    def parse(text):
        jsonobj = json.loads(text)
        print(('{}{}').format('当前状态:', jsonobj.get('message')))
        
        status = jsonobj.get('status')  # 获取状态代码(200是正常)
        if status == '200':
            for x in jsonobj.get('data'):
                print('%s %s' % (x.get('time'), x.get('context')))  
    
    parse(response.text)
                         
def qureycomCode(mailNo):
    urlpara = 'http://www.kuaidi100.com/autonumber/autoComNum?resultv2=1&text={}'
    paga = req.get(urlpara.format(mailNo))
    jsonobj = json.loads(paga.text.decode('utf8'))
    if paga.status_code != 200:
        return None
    
    for item in jsonobj.get('auto'):
        return item.get('comCode')
    
    return None
        
if __name__ == '__main__':
    mailNo = '813422761260' 
    comCode = qureycomCode(mailNo)
    if comCode is None:
        print('{}'.format('查询失败'))
        sys.exit()
    
    getExpress(comCode, mailNo)   

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值