- 帖子
- 9
- 精华
- 0
- 积分
- 28
- 阅读权限
- 10
- 注册时间
- 2018-1-10
- 最后登录
- 2018-1-14
|
crossin先生 发表于 2018-1-11 15:30
了解下JSON格式,可以直接整个转成字典
老师,我看我转成了字典,可是下一步为什么还是失败呢?- # -*- coding:gbk -*-
- import urllib2
- import json
- from city import city
- from StringIO import StringIO
- import gzip
- cityname=raw_input('你想查哪个城市的天气?\n')
- citycode=city.get(cityname)
- print citycode
- if citycode:
- try:
- url=('http://wthrcdn.etouch.cn/weather_mini?citykey=%s'%citycode)
- print url
- content=urllib2.urlopen(url).read()
- buf = StringIO(content)
- f = gzip.GzipFile(fileobj=buf)
- data=f.read()
- print data
- print type(data)
- data2=json.loads(data)
- print data2
- print type(data2)
- data3=data2['forecast']
- print data3
- except:
- print '查询失败'
- else:
- print'没有找到该城市'
复制代码 输出的结果是这样的:- 你想查哪个城市的天气?
- 北京
- 101010100
- http://wthrcdn.etouch.cn/weather_mini?citykey=101010100
- {"data":{"yesterday":{"date":"11日星期四","high":"高温 -1℃","fx":"北风","low":"低温 -10℃","fl":"<![CDATA[<3级]]>","type":"晴"},"city":"北京","aqi":"48","forecast":[{"date":"12日星期五","high":"高温 -1℃","fengli":"<![CDATA[<3级]]>","low":"低温 -10℃","fengxiang":"北风","type":"晴"},{"date":"13日星期六","high":"高温 2℃","fengli":"<![CDATA[<3级]]>","low":"低温 -9℃","fengxiang":"西南风","type":"晴"},{"date":"14日星期天","high":"高温 4℃","fengli":"<![CDATA[<3级]]>","low":"低温 -7℃","fengxiang":"南风","type":"多云"},{"date":"15日星期一","high":"高温 5℃","fengli":"<![CDATA[<3级]]>","low":"低温 -5℃","fengxiang":"西南风","type":"晴"},{"date":"16日星期二","high":"高温 4℃","fengli":"<![CDATA[<3级]]>","low":"低温 -4℃","fengxiang":"东北风","type":"多云"}],"ganmao":"昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。","wendu":"-9"},"status":1000,"desc":"OK"}
- <type 'str'>
- {u'status': 1000, u'data': {u'city': u'\u5317\u4eac', u'yesterday': {u'fx': u'\u5317\u98ce', u'type': u'\u6674', u'high': u'\u9ad8\u6e29 -1\u2103', u'low': u'\u4f4e\u6e29 -10\u2103', u'date': u'11\u65e5\u661f\u671f\u56db', u'fl': u'<![CDATA[<3\u7ea7]]>'}, u'forecast': [{u'fengxiang': u'\u5317\u98ce', u'high': u'\u9ad8\u6e29 -1\u2103', u'fengli': u'<![CDATA[<3\u7ea7]]>', u'date': u'12\u65e5\u661f\u671f\u4e94', u'type': u'\u6674', u'low': u'\u4f4e\u6e29 -10\u2103'}, {u'fengxiang': u'\u897f\u5357\u98ce', u'high': u'\u9ad8\u6e29 2\u2103', u'fengli': u'<![CDATA[<3\u7ea7]]>', u'date': u'13\u65e5\u661f\u671f\u516d', u'type': u'\u6674', u'low': u'\u4f4e\u6e29 -9\u2103'}, {u'fengxiang': u'\u5357\u98ce', u'high': u'\u9ad8\u6e29 4\u2103', u'fengli': u'<![CDATA[<3\u7ea7]]>', u'date': u'14\u65e5\u661f\u671f\u5929', u'type': u'\u591a\u4e91', u'low': u'\u4f4e\u6e29 -7\u2103'}, {u'fengxiang': u'\u897f\u5357\u98ce', u'high': u'\u9ad8\u6e29 5\u2103', u'fengli': u'<![CDATA[<3\u7ea7]]>', u'date': u'15\u65e5\u661f\u671f\u4e00', u'type': u'\u6674', u'low': u'\u4f4e\u6e29 -5\u2103'}, {u'fengxiang': u'\u4e1c\u5317\u98ce', u'high': u'\u9ad8\u6e29 4\u2103', u'fengli': u'<![CDATA[<3\u7ea7]]>', u'date': u'16\u65e5\u661f\u671f\u4e8c', u'type': u'\u591a\u4e91', u'low': u'\u4f4e\u6e29 -4\u2103'}], u'ganmao': u'\u663c\u591c\u6e29\u5dee\u8f83\u5927\uff0c\u8f83\u6613\u53d1\u751f\u611f\u5192\uff0c\u8bf7\u9002\u5f53\u589e\u51cf\u8863\u670d\u3002\u4f53\u8d28\u8f83\u5f31\u7684\u670b\u53cb\u8bf7\u6ce8\u610f\u9632\u62a4\u3002', u'wendu': u'-9', u'aqi': u'48'}, u'desc': u'OK'}
- <type 'dict'>
- 查询失败
复制代码 |
|