- 帖子
- 7
- 精华
- 0
- 积分
- 35
- 阅读权限
- 10
- 注册时间
- 2017-9-21
- 最后登录
- 2018-3-20
|
- # -*- coding: cp936 -*-
- import urllib2
- import json
- from city import city
- cityname = raw_input('你想查哪个城市的天气?\n')
- citycode = city.get(cityname)
- if citycode:
- try:
- url = ('http://www.weather.com.cn/data/cityinfo/%s.html'
- % city_code)
- content = urllib2.urlopen(url).read()
- data = json.loads(content)
- result = data['weatherinfo']
- str_temp = ('%s\n%s ~ %s')%(
- result['weather'],
- result['temp1'],
- result['tmep2']
- )
- print str_temp
- except:
- print '查询失败'
- else:
- print '没有找到该城市'
复制代码- Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
- Type "copyright", "credits" or "license()" for more information.
- >>> ================================ RESTART ================================
- >>>
- 你想查哪个城市的天气?
- 北京
- 查询失败
- >>> ================================ RESTART ================================
- >>>
- 你想查哪个城市的天气?
- 西安
- 查询失败
- >>> ================================ RESTART ================================
- >>>
- 你想查哪个城市的天气?
- 什么鬼
- 没有找到该城市
- >>>
复制代码 |
|