python# 导入requests库,用于发送HTTP请求
import requests
import json
# POST请求的URL,这里假设你有一个本地服务器在192.168.1.133的3001端口上运行,用于接收POST请求
webhook_url = 'http://192.168.1.133:3001/webhook/msg'
# 定义一个函数,用于发送GET请求获取天气数据
def get_weather_data(city_code):
# 构建请求URL,其中city_code是城市代码,用于指定要查询的城市
url = f"http://t.weather.sojson.com/api/weather/city/{city_code}"
# 发送GET请求
response = requests.get(url)
# 如果响应状态码不是200(即请求成功),则返回错误信息
if response.status_code != 200:
return f"请求失败,状态码:{response.status_code}"
# 尝试解析响应内容为JSON格式
try:
weather_data = response.json()
# 提取城市名称
city_name = weather_data['cityInfo']['city']
# 提取当前温度
wendu = weather_data['data']['wendu']
# 提取天气状况
weather_description = weather_data['data']['forecast'][0]['type']
# 提取空气质量
quality = weather_data['data']['quality']
# 提取PM2.5和PM10
pm25 = weather_data['data']['pm25']
pm10 = weather_data['data']['pm10']
# 提取其他信息
forecast = weather_data['data']['forecast'][0]['notice']
# 返回格式化的天气信息字符串
return f"天气推送 城市:{city_name}\n 当前温度:{wendu}℃\n 天气:{weather_description}\n 空气质量:{quality}\n PM2.5:{pm25}\n PM10:{pm10}\n 预报:{forecast}\n 信息来自中国气象网查询"
except json.JSONDecodeError as e:
# 如果JSON解析失败,返回错误信息
return f"解析JSON失败:{e}"
# 定义一个函数,用于发送POST请求
def send_post_request(content, recipient):
# 设置请求头,指定内容类型为JSON
headers = {
'Content-Type': 'application/json'
}
# 构建POST请求的数据,包括接收者、消息类型和内容
payload = {
"to": recipient, # 使用传入的recipient参数作为接收者
"type": "text",
"content": content
}
# 发送POST请求,并将数据以JSON格式发送
response = requests.post(webhook_url, headers=headers, data=json.dumps(payload))
# 打印响应状态码
print(f"Response status code: {response.status_code}")
# 主函数,程序的入口点
if __name__ == '__main__':
city_code = '101080913' # 北京的城市代码
weather_info = get_weather_data(city_code)
# 假设接收者名称为"艾米拉特工"
recipients = ["demo01", "demo02", "demo03"]
for recipient in recipients:
# 发送消息给当前的接收者
send_post_request(weather_info, recipient)
requests:用于发送HTTP请求。
json:用于处理JSON数据。
Webhook URL:webhook_url:这是你的Webhook服务的URL,用于接收POST请求。在这个例子中,它指向本地网络的IP地址192.168.1.133上的端口3001。
get_weather_data(city_code):这个函数接受一个参数city_code,这是城市的天气代码。
使用requests.get发送GET请求到API URL,该URL包含了城市代码。
检查HTTP响应的状态码,如果请求失败,返回错误信息。
解析JSON响应数据,提取城市名称、当前温度、天气状况、空气质量、PM2.5和PM10值,以及预报通知。
pythonreturn f"天气推送 城市:{city_name}, 当前温度:{wendu}℃, 天气:{weather_description}, 空气质量:{quality}, PM2.5:{pm25}, PM10:{pm10}, 预报:{forecast} 信息来自中国气象网查询"
可以自定义return f内的字符,实现自定义消息文本的效果
如果解析JSON时出现错误,捕获json.JSONDecodeError并返回错误信息。
返回一个包含所有提取信息的字符串。
send_post_request(content):设置请求头,指定内容类型为pplication/json。
创建一个包含接收者标识、消息类型和内容的字典payload。
使用requests.post发送POST请求到Webhook URL,传递payload作为JSON数据。
打印Webhook服务的响应状态码。
设置城市代码city_code为01010100,这是北京的城市代码。
调用get_weather_data函数获取天气信息。
使用一个列表储存接收者的名称,再使用for循环遍历列表,实现给多个接收者发生的功能
调用send_post_request函数将天气信息发送到Webhook。
其中01010100是城市的代码,获得城市代码进入: http://www.weather.com.cn
在搜索框上输入你要需要获得天气的城市,点击查询,即可在地址栏获得相应城市编号,然后替换: http://m.weather.com.cn/data/01010100.html

本文作者:Casear
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!