Python 天气预报

# !/usr/bin/env python
# coding=utf-8
import httplib
import json

httpClient = None
try:
    httpClient = httplib.HTTPConnection('api.map.baidu.com', 80, timeout=30)
    httpClient.request('GET',
                       '/telematics/v3/weather?location=%E6%AD%A6%E6%B1%89&output=json&ak=KPGX6sBfBZvz8NlDN5mXDNBF&callback=')

    response = httpClient.getresponse()
    s = json.loads(response.read());
    print s["results"][0]["currentCity"]
    print s["results"][0]["weather_data"][0]["date"]
    print s["results"][0]["weather_data"][0]["weather"]
    print s["results"][0]["weather_data"][0]["wind"]
    print s["results"][0]["weather_data"][0]["temperature"]

except Exception, e:
    print e
finally:
    if httpClient:
        httpClient.close()

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/wangms/PycharmProjects/Python/weather/weather.py
武汉
周五 08月04日 (实时:33℃)
阵雨转多云
无持续风向微风
35 ~ 27℃

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/clarence20170301/article/details/76666966