pampas

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

通过中国天气网的通用接口查询天气。

#coding:utf-8

import urllib2, json
from city import city

yourcity = raw_input("你想查那个城市的天气?")
#yourcity = '杭州'

url = "http://www.weather.com.cn/data/cityinfo/" + city[yourcity] + ".html"

response = urllib2.urlopen(url, timeout=10)
city_dict = response.read()

jsondata = json.JSONDecoder().decode(city_dict)
print jsondata

temp_low = jsondata['weatherinfo']['temp1']
print temp_low
temp_high = jsondata['weatherinfo']['temp2']
weather = jsondata['weatherinfo']['weather']

print weather
print temp_low + "~" + temp_high 

 

posted on 2014-12-25 11:23  pampas  阅读(408)  评论(0编辑  收藏  举报