python3环境下运行3-2代码报错
来源:3-2 如何实现可迭代对象和迭代器对象(2)
蜗v牛
2017-05-22
请问python3 运行下面代码会出错,如何解决?
import requests
def getWeather(city):
r = requests.get(u'http://wthrcdn.etouch.cn/weather_mini?city=' + city)
data = r.json()['forecast'][0]
return '%s: %s , %s ' % (city, data['low'], data['high'])
print(getWeather(u'北京'))
print(getWeather(u'上海'))
错误信息:
data = r.json()['forecast'][0]
KeyError: 'forecast'
写回答
2回答
-
data = r.json()['data']['forecast'][0]
json() 后面加上['data'] 试试
112017-06-05 -
程序员硕
2017-05-23
加打印自己分析. 你需要学习的是方法, 而不是看到一个跑正确的程序.
00
相似问题