⚙️ 参数设置
💡 使用说明:
1. 输入要查询的城市名称
2. 点击查询按钮
3. 返回天气、温度、风向等信息

注意:需要申请阿里云天气API

🌤️ 查询天气

请输入城市名称并点击查询

import urllib.request
import sys
import json

host = 'http://jisutqybmf.market.alicloudapi.com'
path = '/weather/query'
method = 'GET'
appcode = 'YOUR_APP_CODE'  # 替换为你的AppCode

# 获取用户输入
city = input("请输入要查询的城市:")
# URL编码
city = urllib.request.quote(city)

# 构建请求
querys = 'city=' + city
url = host + path + '?' + querys
request = urllib.request.Request(url)
request.add_header('Authorization', 'APPCODE ' + appcode)

# 发送请求
response = urllib.request.urlopen(request)
content = response.read()
info = content.decode('utf-8')
info = json.loads(info)

# 输出天气信息
print(info['result']['week'])
print(info['result']['weather'])
print(info['result']['temp'])
print(info['result']['winddirect'])
本地运行方法:
  1. 注册阿里云市场账号
  2. 购买天气查询API服务
  3. 获取 AppCode
  4. 修改代码中的 appcode 为你的密钥
  5. 运行:python weather.py
API申请地址:

阿里云天气API