zoukankan      html  css  js  c++  java
  • Python # 和风天气接口

    ###   备注只是好奇测试了下。

        和风天气官网地址:https://www.heweather.com/

        和风天气接口注册后可以免费试用。

     服务器节点接口地址
    付费用户 中国、北美、欧洲、东南亚 https://api.heweather.com/v5/
    免费用户 中国 https://free-api.heweather.com/v5/
    # -*- coding: utf-8 -*-
    import urllib2,json
    #调用和风天气的API city可以通过https://cdn.heweather.com/china-city-list.txt城市列表获取
    url = 'https://free-api.heweather.com/v5/weather?city=CN101230201&key=8a439a7e0e034cdcb4122c918f55e5f3'
    #用urllib2创建一个请求并得到返回结果
    req = urllib2.Request(url)
    resp = urllib2.urlopen(req).read()
    # print resp
    # print type(resp)
    
    #将JSON转化为Python的数据结构
    json_data = json.loads(resp)
    city_data=json_data['HeWeather5'][0]
    hourly_data= json_data['HeWeather5'][0]['hourly_forecast']
    daily_data = json_data['HeWeather5'][0]['daily_forecast']
    print json_data
    print u'当前时间:' + daily_data[0]['date']
    print u'城市:' + city_data['basic']['city']
    print u'PM指数:' + city_data['aqi']['city']['pm25']
    print u'白天天气:' + daily_data[0]['cond']['txt_d']
    print u'夜间天气:' + daily_data[0]['cond']['txt_n']
    print u'今天{0}: 气温:{1}°/{2}°'.format(str(daily_data[0]['date']),daily_data[0]['tmp']['min'],daily_data[0]['tmp']['max'])
    print u'未来小时天气:{0} {1}'.format(str(hourly_data[0]['date']).split()[1],hourly_data[0]['cond']['txt'])
    print u'未来小时天气:{0} {1}'.format(str(hourly_data[1]['date']).split()[1],hourly_data[1]['cond']['txt'])
    print u'未来小时天气:{0} {1}'.format(str(hourly_data[2]['date']).split()[1],hourly_data[2]['cond']['txt'])
    print u'未来{0} 天气:{1}°/{2}°'.format(daily_data[1]['date'],daily_data[1]['tmp']['min'],daily_data[1]['tmp']['max'])
    print u'未来{0} 天气:{1}°/{2}°'.format(daily_data[2]['date'],daily_data[1]['tmp']['min'],daily_data[2]['tmp']['max'])
    print u'穿衣建议:' + json_data['HeWeather5'][0]['suggestion']['drsg']['txt']

    ###

  • 相关阅读:
    CodeForces 97 E. Leaders(点双连通分量 + 倍增)
    51nod 1318 最大公约数与最小公倍数方程组(2-SAT)
    关于 atcoder 页面美化的 css
    凸优化小结
    LOJ #2802. 「CCC 2018」平衡树(整除分块 + dp)
    AGC 016 F
    BZOJ 3745: [Coci2015]Norma(分治)
    BZOJ 1124: [POI2008]枪战Maf(构造 + 贪心)
    Linux之Json20160705
    Linux之ioctl20160705
  • 原文地址:https://www.cnblogs.com/lwsup/p/7535670.html
Copyright © 2011-2022 走看看