Python invokes the public API of China Weather Network to get weather information

Source: Internet
Author: User
There is nothing to do to get the weather API, roughly checked, the domestic relatively good or Chinese weather Network (China Meteorological station), the portal has Sina and Tencent (search), other some small sites worry about instability, so no use.

The simplest script. Send a request to get JSON and parse it and simply output it to the screen.

This is only used in the Beijing area, and other areas need to replace the relevant code in the URL address. The code can be found in the reference link below.

Screenshot below, and there are garbled, no way, python garbled too disgusting, in the idle run is normal, do it:


Reference Links:

Http://www.weste.net/2012/8-23/84850.html



#!  /usr/bin/python # coding = utf-8 # Todo:get weather info from weather.com.cn # author:steven # DATE:2013/05/13 Import URLLIB2 Import JSON # get weather HTML and parse to JSON weatherhtml = Urllib2.urlopen (' http://m.weather.com.cn/data/101 010100.html '). Read () Weatherjson = json. Jsondecoder (). Decode (weatherhtml) weatherinfo = weatherjson[' Weatherinfo '] # print weather info print ' City: \ t ', Weatherin fo[' city '] print ' time: \ t ', weatherinfo[' date_y '] print ' 24-hour weather: ' print ' temperature: \ t ', weatherinfo[' Temp1 '] print ' weather: \ t ', weathe rinfo[' weather1 '] print ' wind speed: \ t ', weatherinfo[' wind1 '] print ' ultraviolet: \ t ', weatherinfo[' index_uv '] print ' clothing index: \ t ', weatherinfo[' index_d '] print ' 48-hour weather: ' print ' temperature: \ t ', weatherinfo[' temp2 '] print ' weather: \ t ', weatherinfo[' weather2 '] Print
' Wind speed: \ t ', weatherinfo[' wind2 '] print ' UV: \ t ', weatherinfo[' index48_uv '] print ' dressing index: \ t ', weatherinfo[' index48_d ' print ' 72-hour weather: ' print ' temperature: \ t ', weatherinfo[' Temp3 '] print ' weather: \ t ', weatherinfo[' Weather3 '] print ' wind speed: \ t ', weatherinfo[' Wind3 '] 




Related Article

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.