美文网首页
python 获取天气信息

python 获取天气信息

作者: 谢小帅 | 来源:发表于2018-12-12 16:53 被阅读42次

    get_weather.py

    import json, requests, pprint
    
    weatherJsonUrl = "http://wthrcdn.etouch.cn/weather_mini?city=杭州"  # 将链接定义为一个字符串
    response = requests.get(weatherJsonUrl)  # 获取并下载页面,其内容会保存在respons.text成员变量里面
    response.raise_for_status()  # 这句代码的意思如果请求失败的话就会抛出异常,请求正常就上面也不会做
    
    # 将json文件格式导入成python的格式
    weatherData = json.loads(response.text)
    pprint.pprint(weatherData)
    
    weather_dict = dict()
    weather_dict['high'] = weatherData['data']['forecast'][0]['high']
    weather_dict['low'] = weatherData['data']['forecast'][0]['low']
    weather_dict['type'] = weatherData['data']['forecast'][0]['type']
    weather_dict['fengxiang'] = weatherData['data']['forecast'][0]['fengxiang']
    weather_dict['ganmao'] = weatherData['data']['ganmao']
    pprint.pprint(weather_dict)
    

    相关文章

      网友评论

          本文标题:python 获取天气信息

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