Python get live weather and 7-day forecast

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.