The country's many weather apps, as well as the weather and forecast information provided by the website, are the final source of the official meteorological office. Of course, there are some software and web site data sources are foreign meteorological agencies.
Then we can get it ourselves. The Chinese weather NET provides a free data interface, but I use the interface provided by the wind weather, and the original format for getting the data is Json , which can be easily used as a Python dictionary type.
The data includes city information, weather conditions (about half an hour or so), air quality, and 7-day detailed forecasts.
Need a Baidu Apikey, to register a bit can be obtained. Click "Get Apikey" on the http://apistore.baidu.com/apiworks/servicedetail/478.html page
1 #!/usr/bin/env Python32 ImportJSON3 Importurllib.request4 5 6 defGetcityweather (CityName, BDAPI):7 " "8 cityname: ' Xian ', Bdapi: Baidu Apikey9 " "TenURL ='http://apis.baidu.com/heweather/weather/free?city='+ CityName#HTTPS can also OneRequest =urllib.request.Request (URL) ARequest.add_header ('Apikey', Bdapi) -Response =Urllib.request.urlopen (Request) -Weatherinfo = Response.read (). Decode ('Utf-8') theJsondatas =json.loads (weatherinfo) - forKinchJsondatas.keys ():#there is actually only one key-value pair -Dictdatas =Jsondatas[k][0] - returnDictdatas + - + if __name__=='__main__': ADictdatas = Getcityweather ('Xian','xxxxxxxxxxxxxxxxxxxxxxxxxxx')#Replace city and Apikey at Print(dictdatas['Basic'][' City'])#Place Names - Print('Update Time:', dictdatas['Basic']['Update']['Loc'])#Update Time - Print('Live:', dictdatas[' Now'])#Live Data - if 'AQI' inchDictdatas:#Some places do not have AQI data, such as county area - Print('AQI:', dictdatas['AQI'][' City'])
Execution Result:
Xian update Time:2015-12-22 20:05Live: {'tmp':'5','PCPN':'0','Hum':' -','FL':'2',' Wind': {'dir':'Dongfeng','SPD':' +','SC':'4-5','deg':'Ten'},'Cond': {'Code':'502','txt':'Haze'},'pres':'1024x768','Vis':'1'}aqi: {'qlty':'Heavy pollution','PM25':'194','O3':' the','SO2':' +','Co':'3','AQI':'245','NO2':' the','PM10':'328'}
Python get live weather and 7-day forecast