python练习之获取天气预报

2018-09-11  本文已影响0人  就是很皮
myheart.png

好久没有写简书了 最近在学python 树莓派暂时闲置了 这几天学python学到查天气这边 但是因为接口都失效了 我就自己用了api接口来替代繁琐的操作

一、申请API接口

API接口是什么我就不阐述了 具体自行百度

请求地址:http://v.juhe.cn/weather/index
请求参数:cityname=%E8%8B%8F%E5%B7%9E&dtype=&format=&key=AppKey
请求方式:GET

二、准备写抓取天气的代码

特地说明一下 我就是个菜鸟 今天上课的时候花了好久才写出来 因为很多还没有学过 我百度了好久才写出来的 而且遇到了不少的坑

完整代码:

#-*- coding=utf-8 -*-
import sys
import json
import urllib2
import urllib
appkey = '********'#你的key
cityname = raw_input('请输入城市名称:')#定义cityname
cityname = cityname.encode('utf-8')#把cityname用utf-8编码
cityname = cityname.decode('gb2312')#把cityname用gb2312解码
cityname = urllib.quote(cityname)#把cityname转成url编码
get_city_url = ('http://v.juhe.cn/weather/index?format=2&cityname=%s&key=%s' % (cityname,appkey))#接口地址
get_city_json = urllib2.urlopen(get_city_url).read()#读取接口信息
get_city_json_except = json.loads(get_city_json)#把json格式字符串转成字典
try:#异常处理 因为这个接口参数不能输入xx省 不然直接报错 所以直接提示用户输入地级市
   get_main = get_city_json_except['result']['today']#访问result的today值 下面的温度什么都包括在today这里
   get_temp = get_main['temperature']#获取温度
   get_we = get_main['weather']#获取天气情况
   get_wind = get_main['wind']#获取风向
   message = [get_temp,get_we,get_wind]#把温度 天气情况 风向存在一个list中
   for city_get in message:#遍历list中的每一项
      print city_get#输出每一项
except:
   print '请输入正确地级市'.decode('utf-8')

代码执行效果:

bj6.png

json返回的信息:

{
    "resultcode":"200",
    "reason":"查询成功",
    "result":{
        "sk":{
            "temp":"24",
            "wind_direction":"东南风",
            "wind_strength":"1级",
            "humidity":"78%",
            "time":"17:38"
        },
        "today":{
            "temperature":"23℃~28℃",
            "weather":"阴转多云",
            "weather_id":{
                "fa":"02",
                "fb":"01"
            },
            "wind":"东北风3-5级",
            "week":"星期二",
            "city":"苏州",
            "date_y":"2018年09月11日",
            "dressing_index":"炎热",
            "dressing_advice":"天气炎热,建议着短衫、短裙、短裤、薄型T恤衫等清凉夏季服装。",
            "uv_index":"弱",
            "comfort_index":"",
            "wash_index":"较适宜",
            "travel_index":"较适宜",
            "exercise_index":"较适宜",
            "drying_index":""
        },
        "future":{
            "day_20180911":{
                "temperature":"23℃~28℃",
                "weather":"阴转多云",
                "weather_id":{
                    "fa":"02",
                    "fb":"01"
                },
                "wind":"东北风3-5级",
                "week":"星期二",
                "date":"20180911"
            },
            "day_20180912":{
                "temperature":"24℃~30℃",
                "weather":"多云转阴",
                "weather_id":{
                    "fa":"01",
                    "fb":"02"
                },
                "wind":"东北风4-5级",
                "week":"星期三",
                "date":"20180912"
            },
            "day_20180913":{
                "temperature":"25℃~29℃",
                "weather":"阵雨转阴",
                "weather_id":{
                    "fa":"03",
                    "fb":"02"
                },
                "wind":"东北风3-5级",
                "week":"星期四",
                "date":"20180913"
            },
            "day_20180914":{
                "temperature":"25℃~32℃",
                "weather":"多云",
                "weather_id":{
                    "fa":"01",
                    "fb":"01"
                },
                "wind":"西风3-5级",
                "week":"星期五",
                "date":"20180914"
            },
            "day_20180915":{
                "temperature":"25℃~32℃",
                "weather":"多云",
                "weather_id":{
                    "fa":"01",
                    "fb":"01"
                },
                "wind":"东北风3-5级",
                "week":"星期六",
                "date":"20180915"
            },
            "day_20180916":{
                "temperature":"25℃~32℃",
                "weather":"多云",
                "weather_id":{
                    "fa":"01",
                    "fb":"01"
                },
                "wind":"西风3-5级",
                "week":"星期日",
                "date":"20180916"
            },
            "day_20180917":{
                "temperature":"25℃~32℃",
                "weather":"多云",
                "weather_id":{
                    "fa":"01",
                    "fb":"01"
                },
                "wind":"西风3-5级",
                "week":"星期一",
                "date":"20180917"
            }
        }
    },
    "error_code":0
}
上一篇 下一篇

猜你喜欢

热点阅读