美文网首页
python day 08获取天气信息.制作天气预报软件

python day 08获取天气信息.制作天气预报软件

作者: michaeljacc | 来源:发表于2016-06-22 22:17 被阅读276次

    API 百度APIstore

    有一个天气api 百度APISTORE 获取验证码,地下有个python代码范例
    今天写的代码

    如果直接通过api得到的结果是一串.人类看不懂

    {"HeWeather data service 3.0":[{"aqi":{"city":{"aqi":"52","co":"1","no2":"58","o3":"36","pm10":"51","pm25":"28","qlty":"良","so2":"8"}},"basic":{"city":"上海","cnty":"中国","id":"CN101020100","lat":"31.213000","lon":"121.445000","update":{"loc":"2016-06-22 21:51","utc":"2016-06-22 13:51"}},"daily_forecast":[{"astro":{"sr":"04:50","ss":"19:01"},"cond":{"code_d":"305","code_n":"104","txt_d":"小雨","txt_n":"阴"},"date":"2016-06-22","hum":"72","pcpn":"16.1","pop":"98","pres":"1004","tmp":{"max":"30","min":"25"},"vis":"10","wind":{"deg":"248","dir":"南风","sc":"微风","spd":"1"}},{"astro":{"sr":"04:51","ss":"19:01"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},"date":"2016-06-23","hum":"65","pcpn":"0.6","pop":"90","pres":"1004","tmp":{"max":"34","min":"26"},"vis":"10","wind":{"deg":"208","dir":"西南风","sc":"微风","spd":"9"}},{"astro":{"sr":"04:51","ss":"19:02"},"cond":{"code_d":"302","code_n":"302","txt_d":"雷阵雨","txt_n":"雷阵雨"},"date":"2016-06-24","hum":"62","pcpn":"18.2","pop":"80","pres":"1002","tmp":{"max":"33","min":"23"},"vis":"10","wind":{"deg":"252","dir":"东风","sc":"微风","spd":"9"}},{"astro":{"sr":"04:51","ss":"19:02"},"cond":{"code_d":"300","code_n":"104","txt_d":"阵雨","txt_n":"阴"},"date":"2016-06-25","hum":"68","pcpn":"0.2","pop":"45","pres":"1008","tmp":{"max":"28","min":"23"},"vis":"10","wind":{"deg":"213","dir":"南风","sc":"微风","spd":"5"}},{"astro":{"sr":"04:52","ss":"19:02"},"cond":{"code_d":"302","code_n":"104","txt_d":"雷阵雨","txt_n":"阴"},"date":"2016-06-26","hum":"89","pcpn":"6.8","pop":"63","pres":"1010","tmp":{"max":"28","min":"24"},"vis":"9","wind":{"deg":"162","dir":"东南风","sc":"微风","spd":"9"}},{"astro":{"sr":"04:52","ss":"19:02"},"cond":{"code_d":"302","code_n":"302","txt_d":"雷阵雨","txt_n":"雷阵雨"},"date":"2016-06-27","hum":"90","pcpn":"18.4","pop":"58","pres":"1006","tmp":{"max":"30","min":"24"},"vis":"7","wind":{"deg":"221","dir":"西南风","sc":"微风","spd":"10"}},{"astro":{"sr":"04:52","ss":"19:02"},"cond":{"code_d":"302","code_n":"302","txt_d":"雷阵雨","txt_n":"雷阵雨"},"date":"2016-06-28","hum":"88","pcpn":"11.7","pop":"50","pres":"1007","tmp":{"max":"27","min":"23"},"vis":"9","wind":{"deg":"255","dir":"东南风","sc":"微风","spd":"7"}}],"hourly_forecast":[{"date":"2016-06-22 22:00","hum":"89","pop":"48","pres":"1005","tmp":"28","wind":{"deg":"260","dir":"西风","sc":"微风","spd":"10"}}],"now":{"cond":{"code":"101","txt":"多云"},"fl":"33","hum":"89","pcpn":"0","pres":"1005","tmp":"26","vis":"10","wind":{"deg":"220","dir":"北风","sc":"微风","spd":"0"}},"status":"ok","suggestion":{"comf":{"brf":"很不舒适","txt":"白天天气晴好,但烈日炎炎和较大的空气湿度会使您感到很闷热,很不舒适。"},"cw":{"brf":"较适宜","txt":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},"drsg":{"brf":"炎热","txt":"天气炎热,建议着短衫、短裙、短裤、薄型T恤衫等清凉夏季服装。"},"flu":{"brf":"少发","txt":"各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。"},"sport":{"brf":"较不宜","txt":"天气较好,但炎热,请适当减少运动时间并降低运动强度,户外运动须注意防晒。"},"trav":{"brf":"较适宜","txt":"天气较好,温度较高,天气较热,但有微风相伴,还是比较适宜旅游的,不过外出时要注意防暑防晒哦!"},"uv":{"brf":"中等","txt":"属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。"}}}]}
    
    
    

    把结果复制到jsbeautifer.org解析,分析结果

    复制进去,点beautiful js or html

    美化网站

    编写程序获取上海的天气结果

    # -*- coding: utf-8 -*-
    import requests
    #导入requests库
    import json
    #导入jason库,是一个把数据变成字典的库
    
    url = 'http://apis.baidu.com/heweather/weather/free?city=shanghai'
    # url 尾巴是城市的名字
    key = '9d2576232a5ccda59d47f67cc672c315'
    # key是在百度apistore上的到的key
    
    headers = {
        'apikey': key
    }
    r = requests.get(url, headers = headers)
    # print(r.text)
    weather_info = json.loads(r.text)
    #用json解析
    # print(weather_info.keys())
    weather_data = weather_info['HeWeather data service 3.0'][0]
    daily_forecast = weather_data['daily_forecast']
    # print(len(weather_data),weather_data)
    # weather_data长度只有1,列表里面只有一个字典元素
    forecast = []
    for f in daily_forecast:
        print('天气',f['date'],f['tmp']['max'], f['tmp']['min'])
        date = f['date']
        max = f['tmp']['max']
        min = f['tmp']['min']
        forecast.append([date, max, min])
    # 获取的结果以数组返回
    print(forecast)
    
    
    

    结果

    [['2016-06-22', '30', '25'],
    ['2016-06-23', '34', '26'],
    ['2016-06-24', '33', '23'], 
    ['2016-06-25', '28', '23'],
    ['2016-06-26', '28', '24'],
    ['2016-06-27', '30', '24'], 
    ['2016-06-28', '27', '23']]
    
    

    输出结果可以作为数据画出折线图, 科科一个这样的程序就这样出来了

    相关文章

      网友评论

          本文标题:python day 08获取天气信息.制作天气预报软件

          本文链接:https://www.haomeiwen.com/subject/fbsndttx.html