day14 python3 天气的接口

import requests

def get_weather(city_code):
    url = 'http://www.weather.com.cn/data/sk/%s.html' % city_code
    r = requests.get(url)
    r.encoding = 'utf8'
    data = r.json()
    output = '风向:%s, 风力: %s, 温度:%s, 湿度:%s' % (
        data['weatherinfo']['WD'],
        data['weatherinfo']['WS'],
        data['weatherinfo']['temp'],
        data['weatherinfo']['SD']
    )
    return output

if __name__ == '__main__':
    city_codes = { '0': '101010100', '1': '101121404'}
    prompt = """(0) 北京
(1) 台儿庄
请选择(0/1): """
    choice = input(prompt)
    print(get_weather(city_codes[choice]))

猜你喜欢

转载自blog.csdn.net/weixin_40447206/article/details/81291191
今日推荐