获取天气数据报错
来源:3-2 如何实现可迭代对象和迭代器对象(2)
章小熊熊
2017-12-02
Traceback (most recent call last):
File "weather.py", line 9, in <module>
print getWeather(u'北京')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordi
nal not in range(128)
写回答
1回答
-
July丶
2017-12-05
def getWeather(city):
r = requests.get(u'http://wthrcdn.etouch.cn/weather_mini?city=' + city)
data = r.json()['data']['forecast'][0]
return '%s: %s , %s' % (city.encode('utf-8'), data[u'low'].encode('utf-8'), data[u'high'].encode('utf-8'))
这样就可以了。
00
相似问题