获取天气数据报错

来源: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'))

这样就可以了。


0
0

Python高效编程技巧实战

精选50个Python案例,源自实战,全面提升Python编程能力

2582 学习 · 360 问题

查看课程