在网上找到一篇《python调用中国天气网的公用API获取天气信息 》它的源码是这样的:
4 | # ToDo: get weather info from weather.com.cn |
11 | # get weather html and parse to json |
13 | weatherJSON = json.JSONDecoder().decode(weatherHtml) |
14 | weatherInfo = weatherJSON[ 'weatherinfo' ] |
17 | print '城市:t' , weatherInfo[ 'city' ] |
18 | print '时间:t' , weatherInfo[ 'date_y' ] |
20 | print '温度:t' , weatherInfo[ 'temp1' ] |
21 | print '天气:t' , weatherInfo[ 'weather1' ] |
22 | print '风速:t' , weatherInfo[ 'wind1' ] |
23 | print '紫外线:t' , weatherInfo[ 'index_uv' ] |
24 | print '穿衣指数:t' , weatherInfo[ 'index_d' ] |
26 | print '温度:t' , weatherInfo[ 'temp2' ] |
27 | print '天气:t' , weatherInfo[ 'weather2' ] |
28 | print '风速:t' , weatherInfo[ 'wind2' ] |
29 | print '紫外线:t' , weatherInfo[ 'index48_uv' ] |
30 | print '穿衣指数:t' , weatherInfo[ 'index48_d' ] |
32 | print '温度:t' , weatherInfo[ 'temp3' ] |
33 | print '天气:t' , weatherInfo[ 'weather3' ] |
34 | print '风速:t' , weatherInfo[ 'wind3' ] |
可是由于接口已经失效,所以无法运行,后来又找到了这篇文章《国家气象局免费天气预报接口API》,然后发现原来有三个接口地址:
http://www.weather.com.cn/data/sk/101010100.html
http://www.weather.com.cn/data/cityinfo/101010100.html
http://m.weather.com.cn/data/101010100.html
于是使用另外一个结口地址,稍微改了一下,终于取到了数。
3 | # ToDo: get weather info from weather.com.cn |
8 | # get weather html and parse to json |
10 | weatherJSON = json.JSONDecoder().decode(weatherHtml) |
11 | weatherInfo = weatherJSON[ 'weatherinfo' ] |
14 | print '城市:t' , weatherInfo[ 'city' ] |
16 | print '温度:t' , weatherInfo[ 'temp' ] |
17 | print '风向:t' , weatherInfo[ 'WD' ] |
18 | print '风速:t' , weatherInfo[ 'WS' ] |
19 | print '湿度:t' , weatherInfo[ 'SD' ] |
20 | print '时间:t' , weatherInfo[ 'time' ] |
21 | print '紫外线:t' , weatherInfo[ 'WSE' ] |
22 | print '穿衣指数:t' , weatherInfo[ 'isRadar' ] |
23 | print '温度:t' , weatherInfo[ 'njd' ] |
24 | print '天气:t' , weatherInfo[ 'qy' ] |
25 | print '风速:t' , weatherInfo[ 'rain' ] |
运行结果:

备注:最后面几个参数我懒得对了,说明文字是不对的。
附:另外一个天气json数据
来源《真正的中国天气api接口xml,json》
作者:蜗牛博客