生活中的乐趣
1.需要导入的包
# 线程中每隔多长时间启动一次 from threading import Timer # 通过机器人对象bot的chats(),friends(),groups(),mps()方法,可以分别获取到机器人的所有聊天对象 from wxpy import * # 定义一个机器人并扫码登陆,并保持登陆状态 bot = Bot(cache_path=True)
2.补充小知识
# 发送文本 my_friend.send('Hello, WeChat!') # 发送图片 my_friend.send_image('my_picture.png') # 发送视频 my_friend.send_video('my_video.mov') # 发送文件 my_friend.send_file('my_file.zip') # 以动态的方式发送图片 my_friend.send('@img@my_picture.png')
3.附上代码
def send_news(): try: # 你朋友的微信名称,不是备注,也不是微信帐号。 my_friend = bot.friends().search(u'xxx')[0] my_friend.send(u"正在测试微信接口API,看到后请忽略,谢谢不打我!") t = Timer(0.5, send_news).start() except: # 你自己的微信名称,不是微信帐号。 my_friend = bot.friends().search('xxx')[0] my_friend.send(u"嗨朋友,你的信息发不出去了!")
4.主函数
if __name__ == "__main__": send_news()
好简单啊~~~不到20行代码,,,python的优雅就在这!上面只是单纯展示的单个微信朋友,换个bot()de 方法就可以了
效果太逼真,不宜展示!
PS:有什么不足之处请指出来,我多多改进