Home >  > 中国天气网 json解析

中国天气网 json解析

1

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

    #! /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/101010100.html').read()  
    weatherJSON = json.JSONDecoder().decode(weatherHtml)  
    weatherInfo = weatherJSON['weatherinfo']  
      
    # print weather info  
    print '城市:t', weatherInfo['city']  
    print '时间:t', weatherInfo['date_y']  
    print '24小时天气:'  
    print '温度:t', weatherInfo['temp1']  
    print '天气:t', weatherInfo['weather1']  
    print '风速:t', weatherInfo['wind1']  
    print '紫外线:t', weatherInfo['index_uv']  
    print '穿衣指数:t', weatherInfo['index_d']  
    print '48小时天气:'  
    print '温度:t', weatherInfo['temp2']  
    print '天气:t', weatherInfo['weather2']  
    print '风速:t', weatherInfo['wind2']  
    print '紫外线:t', weatherInfo['index48_uv']  
    print '穿衣指数:t', weatherInfo['index48_d']  
    print '72小时天气:'  
    print '温度:t', weatherInfo['temp3']  
    print '天气:t', weatherInfo['weather3']  
    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

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

# -*- coding: UTF-8 -*-
# /usr/bin/python
# ToDo: get weather info from weather.com.cn

import urllib2
import json

# get weather html and parse to json
weatherHtml = urllib2.urlopen('http://www.weather.com.cn/data/sk/101010100.html').read()
weatherJSON = json.JSONDecoder().decode(weatherHtml)
weatherInfo = weatherJSON['weatherinfo']

# print weather info
print '城市:t', weatherInfo['city']
print '24小时天气:'
print '温度:t', weatherInfo['temp']
print '风向:t', weatherInfo['WD']
print '风速:t', weatherInfo['WS']
print '湿度:t', weatherInfo['SD']
print '时间:t', weatherInfo['time']
print '紫外线:t', weatherInfo['WSE']
print '穿衣指数:t', weatherInfo['isRadar']
print '温度:t', weatherInfo['njd']
print '天气:t', weatherInfo['qy']
print '风速:t', weatherInfo['rain']

运行结果:
Snap18208

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

附:另外一个天气json数据

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

作者:蜗牛博客

本文暂无标签
Comment (1)
Trackback (0)
  1. 日本蜡烛图技术教程精解 unknowunknow 沙发 2017/04/27 14:12

    一看就是高手

  • 还没有Trackback

发表评论

*

*