python基础教程共60课-第45课查天气3

python基础教程共60课-第45课查天气3

【Python 第45课】 查天气(3)

看一下我们已经拿到的json格式的天气数据: 
{
"weatherinfo": {
"city": "南京",
"cityid": "101190101",
"temp1": "37℃",
"temp2": "28℃",
"weather": "多云",
"img1": "d1.gif",
"img2": "n1.gif",
"ptime": "11:00"
}
}
复制代码

直接在命令行中看到的应该是没有换行和空格的一长串字符,这里我把格式整理了一下。可以看出,它像是一个字典的结构,但是有两层。最外层只有一个key--“weatherinfo”,它的value是另一个字典,里面包含了好几项天气信息,现在我们最关心的就是其中的temp1,temp2和weather。

虽然看上去像字典,但它对于程序来说,仍然是一个字符串,只不过是一个满足json格式的字符串。我们用python中提供的另一个模块json提供的loads方法,把它转成一个真正的字典。 
import json

data = json.loads(content)
复制代码

这时候的data已经是一个字典,尽管在控制台中输出它,看上去和content没什么区别,只是编码上有些不同: 
{u'weatherinfo': {u'city': u'\u5357\u4eac', u'ptime': u'11:00', u'cityid': u'101190101', u'temp2': u'28\u2103', u'temp1': u'37\u2103', u'weather': u'\u591a\u4e91', u'img2': u'n1.gif', u'img1': u'd1.gif'}}
复制代码

但如果你用type方法看一下它们的类型: 
print type(content)
print type(data)
复制代码


就知道区别在哪里了。


之后的事情就比较容易了。 
result = data['weatherinfo']
str_temp = ('%s\n%s ~ %s') % (
result['weather'],
result['temp1'],
result['temp2']
)
print str_temp
复制代码

为了防止在请求过程中出错,我加上了一个异常处理。 
try:
###
###
except:
print '查询失败'
复制代码

以及没有找到城市时的处理: 
if citycode:
###
###
else:
print '没有找到该城市'
复制代码

完整代码: 
# -*- coding: utf-8 -*-
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'
% citycode)
content = urllib2.urlopen(url).read()
data = json.loads(content)
result = data['weatherinfo']
str_temp = ('%s\n%s ~ %s') % (
result['weather'],
result['temp1'],
result['temp2']
)
print str_temp
except:
print '查询失败'
else:

print '没有找到该城市'



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
python基础教程60(基础) 【Python 第1】安装 6 【Python 第2】print 7 【Python 第3】IDE 10 【Python 第4】输入 12 【Python 第5】变量 14 【Python 第6】bool 16 【Python 第7】if 19 【Python 第8】while 23 【Python 第9】random 26 【Python 第10】变量2 28 【Python 第11】逻辑判断 29 【Python 第12】 for循环 30 【Python 第13】字符串 32 【Python 第14】字符串格式化 34 【Python 第15】循环的嵌套 35 【Python 第16】字符串格式化2 37 【Python 第17】类型转换 38 【Python 第18】 bool类型转换 40 【Python 第19】 函数 41 【Python 第21】 函数的参数 46 【Python 第22】 函数应用示例 47 【Python 第23】 if, elif, else 49 【Python 第24】 if的嵌套 54 【Python 第25】 初探list 56 【Python 第26】 操作list 58 【Python 第28】 字符串的分割 65 【Python第29】连接list 71 【Python 第30】字符串的索引和切片 72 【Python 第31】读文件 74 【Python 第32】写文件 76 【Python 第33】处理文件中的数据 77 【Python 第34】 break 83 【Python 第35】 continue 84 【Python 第36】异常处理 87 【Python 第37】字典 90 【Python 第38】模块 93 【Python 第39】用文件保存游戏(1) 96 【Python 第40】用文件保存游戏(2) 98 【Python 第41】用文件保存游戏(3) 101 【Python 第42】函数的默认参数 105 【Python 第43】查天气(1) 107 【Python 第44】查天气(2) 108 【Python45】查天气(3) 111 【Python 第46】查天气(4) 113 【Python 第47】面向对象(1) 116 【Python 第48】面向对象(2) 117 【Python 第49】面向对象(3) 118 【Python 第50】面向对象(4) 120 【Python 第51】 and-or技巧 123 【Python 第52】元组 124 【Python 第53】数学运算 125 【Python 第54】真值表 127 【Python 第55】正则表达式(1) 128 【Python 第56】正则表达式(2) 130 【Python 第57】正则表达式(3) 131 【Python 第58】正则表达式(4) 134 【Python 第59】正则表达式(5) 135 【Python60】随机数 137
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值