天气、快递API调用

天气、快递API调用

import json
import requests
import time
class WithJson():
    def weather(self):
        str_json = self.WeatherAPI()
        string = str_json.replace("'", '"')#替换"'", '"'
        pop_data = json.loads(string)#转python字典
        #pop_data = json.load(f)
        list_pop_data=pop_data['data']['forecast']#读取列表
        #print(pop_data['status'])
        #print(pop_data['desc'])
        city1=(pop_data['data']['city'])
        wendu1=(pop_data['data']['wendu'])
        print('\n')
        print("今日:"+list_pop_data[0]['date'] +" " +city1+" "+wendu1+"°"+list_pop_data[0]['type'])
        print(pop_data['data']['ganmao'])
        rq=(pop_data['data']['yesterday']['date'])
        tq=(pop_data['data']['yesterday']['type'])
        print("*************************************************")
        print(rq + ": " + tq)
        for pop_dict in list_pop_data:
                date1 = pop_dict['date']
                tianqi = pop_dict['type']
                print(date1 + ": " + tianqi)

    def ExpressCheck(self):

            str_json =self.ExpressAPI()#字符串
            string = str_json.replace("'", '"')#替换"'", '"'
            pop_data = json.loads(string)#转python字典

            list_pop_data=pop_data['data']#读取列表
            #print("访问结果:"+pop_data['message'])
            print("快递公司:"+pop_data['com'])
            print("快递单号:"+pop_data['nu'])
            print("*************************************************")

            for pop_dict in list_pop_data:
                    time1 = pop_dict['time']
                    context1 = pop_dict['context']
                    print(time1 + ": " + context1)

    def ExpressAPI(self):

        URL2 = 'http://www.kuaidi100.com/query?type=zhongtong&postid=73116039505988'
        
        try:
            r = requests.get(URL2, params={'ip': '8.8.8.8'}, timeout=1)
            r.raise_for_status()  # 如果响应状态码不是 200,就主动抛出异常
        except requests.RequestException as e:
            print(e)
        else:
            result = r.json()
            #print(result)
            js = json.dumps(result)
            return js


    def WeatherAPI(self):

        URL2 = 'http://wthrcdn.etouch.cn/weather_mini?city=西安'
        try:
            r = requests.get(URL2, params={'ip': '8.8.8.8'}, timeout=1)
            r.raise_for_status()  # 如果响应状态码不是 200,就主动抛出异常
        except requests.RequestException as e:
            print(e)
        else:
            result = r.json()
            #print(result)
            js = json.dumps(result)
            #print(js)
            return js






if __name__ == '__main__':
    WithJson=WithJson()
    WithJson.weather()
    print("*************************************************")
    print('\n')
    print("*************************************************")
    WithJson.ExpressCheck()
    input('Press Enter to exit...')


















在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值