Home >  > 中国天气网 json解析

中国天气网 json解析

0

在网上找到一篇《python调用中国天气网的公用API获取天气信息 》它的源码是这样的:

1#! /usr/bin/python 
2# coding = utf-8 
3   
4# ToDo: get weather info from weather.com.cn 
5# Author: Steven 
6# Date: 2013/05/13 
7   
8import urllib2 
9import json 
10   
11# get weather html and parse to json 
12weatherHtml = urllib2.urlopen('http://m.weather.com.cn/data/101010100.html').read() 
13weatherJSON = json.JSONDecoder().decode(weatherHtml) 
14weatherInfo = weatherJSON['weatherinfo'
15   
16# print weather info 
17print '城市:t', weatherInfo['city'
18print '时间:t', weatherInfo['date_y'
19print '24小时天气:' 
20print '温度:t', weatherInfo['temp1'
21print '天气:t', weatherInfo['weather1'
22print '风速:t', weatherInfo['wind1'
23print '紫外线:t', weatherInfo['index_uv'
24print '穿衣指数:t', weatherInfo['index_d'
25print '48小时天气:' 
26print '温度:t', weatherInfo['temp2'
27print '天气:t', weatherInfo['weather2'
28print '风速:t', weatherInfo['wind2'
29print '紫外线:t', weatherInfo['index48_uv'
30print '穿衣指数:t', weatherInfo['index48_d'
31print '72小时天气:' 
32print '温度:t', weatherInfo['temp3'
33print '天气:t', weatherInfo['weather3'
34print '风速: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

于是使用另外一个结口地址,稍微改了一下,终于取到了数。

1# -*- coding: UTF-8 -*-
2# /usr/bin/python
3# ToDo: get weather info from weather.com.cn
4 
5import urllib2
6import json
7 
8# get weather html and parse to json
9weatherHtml = urllib2.urlopen('http://www.weather.com.cn/data/sk/101010100.html').read()
10weatherJSON = json.JSONDecoder().decode(weatherHtml)
11weatherInfo = weatherJSON['weatherinfo']
12 
13# print weather info
14print '城市:t', weatherInfo['city']
15print '24小时天气:'
16print '温度:t', weatherInfo['temp']
17print '风向:t', weatherInfo['WD']
18print '风速:t', weatherInfo['WS']
19print '湿度:t', weatherInfo['SD']
20print '时间:t', weatherInfo['time']
21print '紫外线:t', weatherInfo['WSE']
22print '穿衣指数:t', weatherInfo['isRadar']
23print '温度:t', weatherInfo['njd']
24print '天气:t', weatherInfo['qy']
25print '风速:t', weatherInfo['rain']

运行结果:
Snap18208

备注:最后面几个参数我懒得对了,说明文字是不对的。

附:另外一个天气json数据

来源《真正的中国天气api接口xml,json

作者:蜗牛博客

暧昧帖

本文暂无标签

发表评论

*

*