You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

30 lines
774 B

#!/usr/bin/env python3
"""
查询天气
"""
import urllib.request
import socket
# 设置超时
socket.setdefaulttimeout(10)
try:
url = "https://wttr.in/Guangzhou?format=%l:+%C+%t+%h+%w"
print("正在查询广州天气...")
with urllib.request.urlopen(url) as f:
weather = f.read().decode('utf-8')
print("\n🌤️ 广州今天的天气:")
print(weather)
except Exception as e:
print(f"查询失败: {e}")
print("\n尝试备用方案...")
# 备用:使用简单格式
try:
url = "https://wttr.in/Guangzhou?0"
with urllib.request.urlopen(url) as f:
weather = f.read().decode('utf-8')
print(weather)
except Exception as e2:
print(f"备用方案也失败: {e2}")