用 Python,查快递

在剁手的同时练手 )

# -*- coding: utf-8 -*-
# @Author: Lily_Sun
# @Date:   2018-08-07 01:31:50
# @Last Modified by:   Lily_Sun
# @Last Modified time: 2018-08-07 06:17:13

import json
import requests
import re

#访问网页返回字典
def visit_api(url):
    res = requests.get(url)
    result_dict = json.loads(res.content.decode('utf-8'))
    return result_dict

#返回公司编号
def get_company(epnum):
    url = 'http://m.kuaidi100.com/autonumber/auto?num=' + str(epnum)
    company = visit_api(url)[0]['comCode']
    return company

#查询
def get_express(epnum,company):
    url = 'http://m.kuaidi100.com/query?type=' + str(company) + '&postid='+ str(epnum) + '&id=1&valicode='
    result_list = visit_api(url)['data']
    #print(result_list)
    for result in result_list:
    print(result['context'])
    print("到达时间: "+result['time'])

def main():
    epnum = input('输入你的快递单号:\n')
    get_express(epnum,get_company(epnum))

if __name__ == '__main__':
    main()