Python3 itchat实现微信定时发送群消息的实例代码
Python  /  管理员 发布于 7年前   447
1,使用微信,定时往指定的微信群里发送指定信息。
2,需要发送的内容使用excel进行维护,指定要发送的微信群名、时间、内容。
1,itchat:这个是主要的工具,用于连接微信个人账号接口。以下是一些相关的知识点网站。
2,xlrd:这个是用来读Excel文件的工具。
3,apscheduler:这个是用来定时调度时间的工具。
# coding=utf-8from datetime import datetimeimport itchatimport xlrdfrom apscheduler.schedulers.background import BlockingSchedulerimport osdef SentChatRoomsMsg(name, context): itchat.get_chatrooms(update=True) iRoom = itchat.search_chatrooms(name) for room in iRoom: if room['NickName'] == name: userName = room['UserName'] break itchat.send_msg(context, userName) print("发送时间:" + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "\n" "发送到:" + name + "\n" "发送内容:" + context + "\n") print("*********************************************************************************") scheduler.print_jobs()def loginCallback(): print("***登录成功***")def exitCallback(): print("***已退出***")itchat.auto_login(hotReload=True, enableCmdQR=True, loginCallback=loginCallback, exitCallback=exitCallback)workbook = xlrd.open_workbook( os.path.join(os.path.dirname(os.path.realpath(__file__)), "chatroomsfile\AutoSentChatroom.xlsx"))# workbook = xlrd.open_workbook("D:\PyCharmCode\AutoLiulishouWechat\chatroomsfile\AutoSentChatroom.xlsx")sheet = workbook.sheet_by_name('Chatrooms')iRows = sheet.nrowsscheduler = BlockingScheduler()index = 1for i in range(1, iRows): textList = sheet.row_values(i) name = textList[0] context = textList[2] float_dateTime = textList[1] date_value = xlrd.xldate_as_tuple(float_dateTime, workbook.datemode) date_value = datetime(*date_value[:5]) if datetime.now() > date_value: continue date_value = date_value.strftime('%Y-%m-%d %H:%M:%S') textList[1] = date_value scheduler.add_job(SentChatRoomsMsg, 'date', run_date=date_value, kwargs={"name": name, "context": context}) print("任务" + str(index) + ":\n" "待发送时间:" + date_value + "\n" "待发送到:" + name + "\n" "待发送内容:" + context + "\n" "******************************************************************************\n") index = index + 1if index == 1: print("***没有任务需要执行***")scheduler.start()
总结
以上所述是小编给大家介绍的Python3 itchat实现微信定时发送群消息的实例代码 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号