python+pushpuls推送天气

一、在pushpuls官网注册后,根据需要选择推送消息情况(建议一对多)。

pushplus(推送加)-微信消息推送平台

二、登录成功后:

1、进入后,会有你的token ,可以复制下来,发送的时候脚本中会用到

2、创建一个群组,群组编码信息后面也会用到,建议不要用特殊字符;

3、创建好群组后,会出现下图,之后可以让好友扫码关注pushpuls公众号来推送消息。

 三、利用百度地图开放API获取指定城市天气信息

webapi | 百度地图API SDK

1、得先在百度地图中创建一个应用,最后点击创建应用。

 2、应用类型可以选择「服务端」。

3、下滑会有一个IP白名单校验,可以直接输入0.0.0.0/0不用做限制,点击提交。

4、在我的应用中就可以看到你的ak,也需要复制保存下来。

5、进入WEB服务API

根据提示下方步骤操作,可以下载行政区的编码,代码中的字典中可以输入自己想要的。

 eg:注意是这一列数据。

 四、 话不多说上源码:

import requests
import json
import datetime


# 可以自己添加新的地点和ID,ID获取方式步骤中有。

addes = {
    '北京海淀': '110108',
    '北京西城': '110102'
}


def get_weather(values, key):
    """
    获取返回的天气信息后,
    针对返回的内容进行处理。
    """
    ak = '在百度地图中获取的ak'
    url = f'https://api.map.baidu.com/weather/v1/?data_type=all&ak={ak}&district_id={values}'  # 北京天气网站
    r = requests.get(url, timeout=30)  # 用requests抓取网页信息
    r.raise_for_status()  # 异常时停止
    r.encoding = r.apparent_encoding  # 编码格式
    html = r.text
    now_all = json.loads(html)['result']['now']  # 当前小时级所有天气,字典格式。
    now_text = now_all['text']  # 当前小时级天气现象
    temperature = now_all['temp']  # 当前小时级温度
    relative_humidity = now_all['rh']  # 当前小时级相对湿度
    somatosensory_temperature = now_all['feels_like']  # 当前小时级体感温度
    wind_level = now_all['wind_class']  # 当前小时级风力等级
    wind_direction = now_all['wind_dir']  # 当前小时级风向描述
    tomorrow_all = json.loads(html)['result']['forecasts'][0]  # 今天整体天气
    text_day = tomorrow_all['text_day']  # 白天天气现象
    text_night = tomorrow_all['text_night']  # 夜晚天气现象
    if text_day == text_night:
        phenomenon = text_day
    else:
        phenomenon = text_day + '转' + text_night
    high = tomorrow_all['high']  # 最高温度
    low = tomorrow_all['low']  # 最低温度
    wc_day = tomorrow_all['wc_day']  # 白天风力
    wd_day = tomorrow_all['wd_day']  # 白天风向
    wc_night = tomorrow_all['wc_night']  # 夜晚风力
    wd_night = tomorrow_all['wd_night']  # 夜晚风向
    date = tomorrow_all['date']  # 时间 格式2022-10-27
    week = tomorrow_all['week']  # 今天是星期几
    now_time = datetime.datetime.now().strftime('%H:%M:%S')  # 当前时间 格式19:40:34
    return '\n' + key + '\n' + '今天是:' + date + week + '\n' + '天气现象:' + phenomenon + '\n' + '温度:' + \
        f'{low}~' + f'{high}℃' + '\n' + '白天风力:' + wc_day + '\n' + '白天风向:' + wd_day + '\n'  \
        + '夜晚风力:' + wc_night + '\n' + '夜晚风向:' + wd_night + '\n===============\n' + f'当前时间{now_time}\n天气现象:' \
        + now_text + '\n' + '温度:' + f'{temperature}℃' + '\n' + '相对湿度:' + f'{relative_humidity}%' + '\n' +  \
        '体感温度:' + f'{somatosensory_temperature}℃' + '\n' + '风力等级:' + wind_level + '\n' + '风向描述:' + wind_direction + '\n'


def all_string():
    all_str = ""
    for key, values in addes.items():
        st = get_weather(values, key)
        all_str = all_str + st  # 遍历字典,拿到多个地方的天气信息,如果只有一个也不影响,注意格式就行。
    return all_str


def send_wechat():
    """fasong"""
    token = '你的token'  # 在pushpush网站中可以找到
    title = '发送过去后的标题可以自定义'
    template = "html"  # 发送格式,建议这用html
    topic = '群组编码'
    content = all_string()
    # print(content)
    url = f"http://www.pushplus.plus/send?token={token}&title={title}&content={content}&template={template}&topic={topic}"
    data = bytes(json.dumps(content, ensure_ascii=False).encode('utf-8'))  # 将数据编码json并转换为bytes型
    response = requests.post(url, data=data)
    result = response.json()  # 将返回信息json解码
    print(result)


if __name__ == '__main__':
    send_wechat()

tips:

之后需要每天推送可以利用第三方设备/jenkins,看自己需求喽!!

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值