注意那些每天给你发晚安的人

版权声明:本文为博主原创文章,转载请附上本博文链接。 https://blog.csdn.net/weixin_41704182/article/details/88299243

t a l k i s c h e a p , s h o w m e t h e c o d e talk -is -cheap ,show- me- the- code

  • 安装wxpy
    在这里默认大家已经安装好了pip,我们只需要安装wxpy这个包就行,这里推荐两种安装方式:
    1、从 PYPI 官方源下载安装

    pip install -U wxpy
    

    2、从豆瓣 PYPI 镜像源下载安装

    pip install -U wxpy -i     "https://pypi.doubanio.com/simple/"
    
  • 登陆微信
    wxpy的使用非常简单,我们在导入wxpy库后,只需要创建一个bot对象,就可以实现微信登陆。

     # 导入模块
     from wxpy import *
     # 初始化机器人,扫码登陆
     bot = Bot()
    

    如果你认为每次都需要扫描二维码很麻烦,可以启用缓存,来保存自己的登录状态:

    bot = Bot(console_qr=0)
    
  • 查询好友或群聊
    我们可以通过Bot.friends 以及Bot.groups 来获取到所有的好友以及聊天群,这里需要注意的是,聊天群需要保存到通讯录中,不然可能会出现找不到聊天群的情况。

    # 获取所有好友
    friends = bot.friends()
    
    # 找到好友,search里面的参数是可以好友昵称、city,province,sex
    friend = bot.friends.search(',,')[0]
    
    # 获取所有聊天群
    groups = bot.groups()
    
    # 找到目标群
    group = groups.search("夜来香")[0]
    
  • 发送信息
    接下来主要介绍一下用户发送消息的类型,目前wxpy 支持发送文本,图片,视频以及文件。

     # 发送文本
     my_friend.send('早啊!')
     
     # 发送图片
     my_friend.send_image('福利.png')
     
     # 发送视频
     my_friend.send_video('福利视频.mov')
     
     # 发送文件
     my_friend.send_file('种子.zip')
     
     # 以动态的方式发送图片
     my_friend.send('@img@福利.png')
    
  • 保持登陆
    1、进入 Python 命令行、让程序保持运行embed()
    2、或者使用堵塞线程

     #仅仅堵塞线程
     # bot.join()
    
  • 功能实现

    from wxpy import *
    from threading import Timer
    
    bot = Bot(console_qr=0)
    friends = bot.friends()# 获取所有好友
    
    def send_news():
        try:
           contents = ['早啊','晚安']
           my_friend =friends.search("00")[0]
           my_friend.send(contents[0])
           # 每3秒,发送1次,时间间隔自己设置,3秒的话分分钟拉黑你
           t = Timer(3, send_news)
    	   t.start()
      except:
     	   #如果好友中不存在此人,就发送信息到文件助手
    	   bot.file_helper.send('啊啊啊,消息没人要')
    
    if __name__ == '__main__':
        send_news()
        bot.join()
    
  • 定时发送
    考虑到有些信息需要诚意,需要定时定点发送(好像用这个,诚意这个问题就有点打折了),因此我们需要写一个具有定时发送功能的函数。

    def ONTIME():
      SECONDS_PER_DAY = 24 * 60 * 60
      from datetime import datetime, timedelta
      #获取当前时间
      curTime = datetime.now()
      #	设置需要发送的时间
      desTime = curTime.replace(hour=13, minute=54, second=50, microsecond=0) 
      delta = desTime - curTime
      skipSeconds = delta.total_seconds() % SECONDS_PER_DAY
      return skipSeconds
      while True:
    	s = ONTIME()
     	time.sleep(s)
        send_news()
    
  • 完整代码

    import time
    from wxpy import *
    from threading import Timer
    
    bot = Bot()
    friends = bot.friends()# 获取所有好友
    
    def send_news():
      try:
      	 #设定发送信息
       	 contents = ['早啊','晚安']
       	 my_friend =friends.search(",,")[0]
         my_friend.send(contents[0])
      except:
         # 如果好友中不存在此人,就发送信息到文件助手
         bot.file_helper.send('啊啊啊,消息没人要')
    
    def ONTIME():
       SECONDS_PER_DAY = 24 * 60 * 60
       from datetime import datetime, timedelta
       curTime = datetime.now()
       #设置发送时间
       desTime = curTime.replace(hour=14, minute=45, second=50, microsecond=0)         
       delta = desTime - curTime
       skipSeconds = delta.total_seconds() % SECONDS_PER_DAY
       return skipSeconds
       
    while True:
       s = ONTIME()
       time.sleep(s)
       send_news()
    bot.join()
    
  • 效果
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41704182/article/details/88299243
今日推荐