python自动给微信好友发送天气预报

参考文章:https://www.jianshu.com/p/2c62e684941f

主要思路:
1.从wxpy获取好友列表,也可发给指定好友
2.创建定时器,也可以随时发送
3.定时器触发函数
4.函数执行,遍历好友列表
5.好友对象执行带参函数,参数为该好友城市
6.函数中请求百度天气接口,得到该好友对应天气数据,解析处理数据,发送天气信息,完成该对象发送。
7.遍历结束,发送完毕

完整代码(代码具体功能参照注释)

from wxpy import *

import requests

from datetime import datetime

import time

from apscheduler.schedulers.blocking import BlockingScheduler#定时框架

bot = Bot(cache_path=True)

tuling = Tuling(api_key='4a0488cdce684468b95591a641f0971d')  #机器人api


def send_weather(location):

    #准备url地址

    path ='http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=你的ak&callback=?'

    url = path % location

    response = requests.get(url)

    result = response.json()

    #如果城市错误就按照长春发送天气

    if result['error'] !=0:

        location ='长春'

        url = path % location

        response = requests.get(url)

        result = response.json()

    str0 = ('    你好!这是今天的天气预报!\n机器人:PyChatBot\n')

    results = result['results']

    # 取出数据字典

    data1 = results[0]

    # 取出城市

    city = data1['currentCity']

    str1 ='    你的城市: %s\n' % city

    # 取出pm2.5值

    pm25 = data1['pm25']

    str2 ='    Pm值    : %s\n' % pm25

    # 将字符串转换为整数 否则无法比较大小
    pm25 = int(pm25)

    if pm25 =='':

        pm25 =0


    # 通过pm2.5的值大小判断污染指数

    if 0 <= pm25 <35:

        pollution ='优'

    elif 35 <= pm25 <75:

        pollution ='良'

    elif 75 <= pm25 <115:

        pollution ='轻度污染'

    elif 115 <= pm25 <150:

        pollution ='中度污染'

    elif 150 <= pm25 <250:

        pollution ='重度污染'

    elif pm25 >=250:

        pollution ='严重污染'

    str3 ='    污染指数: %s\n' % pollution

    result1 = results[0]

    weather_data = result1['weather_data']

    data = weather_data[0]

    temperature_now = data['date']

    str4 ='    当前温度: %s\n' % temperature_now

    wind = data['wind']

    str5 ='    风向    : %s\n' % wind

    weather = data['weather']

    str6 ='    天气    : %s\n' % weather

    str7 ='    温度    : %s\n' % data['temperature']

    message = data1['index']

    str8 ='    穿衣    : %s\n' % message[0]['des']

    str9 ='    我很贴心: %s\n' % message[2]['des']

    str10 ='    运动    : %s\n' % message[3]['des']

    str11 ='    紫外线 : %s\n' % message[4]['des']

    str12="\n祝您生活愉快--微笑向暖\n"

    str = str0 + str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8 + str9 + str10 + str11+ str12

    return str

#好友列表

#my_friends = []

#my_friends = bot.friends()

#my_friends.pop(0)

friend = bot.friends().search('好友昵称或好友备注')[0]#好友的微信昵称,或者你存取的备注
print(friend)

#发送函数

def send_message():

    #给全体好友发送------------------
    # for friend in my_friends:
    #     friend.send(send_weather(friend.city))
    #-------------------------------------


    #给单个好友发送消息-----------------
    friend.send(send_weather(friend.city))
    #发送成功通知我

    bot.file_helper.send(send_weather('长春'))

    bot.file_helper.send('发送完毕')

#定时器

print('star')
"""
#定时操作
sched = BlockingScheduler()
sched.add_job(send_message,'cron',month='1-12',day='1-31',hour=15,minute =49)#设定发送的时间
sched.start()
"""
send_message()#执行程序时直接发送

运行效果:
在这里插入图片描述

发送成功后,文件助手有所提示:

在这里插入图片描述

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乐亦亦乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值