Python3 itchat實現(xiàn)微信定時發(fā)送群消息的實例代碼
一、簡介
1,使用微信,定時往指定的微信群里發(fā)送指定信息。
2,需要發(fā)送的內(nèi)容使用excel進行維護,指定要發(fā)送的微信群名、時間、內(nèi)容。
二、py庫
1,itchat:這個是主要的工具,用于連接微信個人賬號接口。以下是一些相關(guān)的知識點網(wǎng)站。
2,xlrd:這個是用來讀Excel文件的工具。
3,apscheduler:這個是用來定時調(diào)度時間的工具。
三、實例代碼
# coding=utf-8
from datetime import datetime
import itchat
import xlrd
from apscheduler.schedulers.background import BlockingScheduler
import os
def 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("發(fā)送時間:" + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "\n"
"發(fā)送到:" + name + "\n"
"發(fā)送內(nèi)容:" + 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.nrows
scheduler = BlockingScheduler()
index = 1
for 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("任務(wù)" + str(index) + ":\n"
"待發(fā)送時間:" + date_value + "\n"
"待發(fā)送到:" + name + "\n"
"待發(fā)送內(nèi)容:" + context + "\n"
"******************************************************************************\n")
index = index + 1
if index == 1:
print("***沒有任務(wù)需要執(zhí)行***")
scheduler.start()
總結(jié)
以上所述是小編給大家介紹的Python3 itchat實現(xiàn)微信定時發(fā)送群消息的實例代碼 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
python調(diào)用攝像頭拍攝數(shù)據(jù)集
這篇文章主要為大家詳細介紹了Python調(diào)用攝像頭拍攝數(shù)據(jù)集,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06
使用Python Typing模塊提升代碼可讀性和健壯性實例探索
這篇文章主要為大家介紹了使用Python Typing模塊提升代碼可讀性和健壯性實例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
使用python采集Excel表中某一格數(shù)據(jù)
這篇文章主要介紹了使用python采集Excel表中某一格數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05
Python實現(xiàn)批量檢測HTTP服務(wù)的狀態(tài)
本文給大家分享的是一個使用python實現(xiàn)的批量檢測web服務(wù)可用性的腳本代碼,主要功能有測試一組url的可用性(可以包括HTTP狀態(tài)、響應(yīng)時間等)并統(tǒng)計出現(xiàn)不可用情況的次數(shù)和頻率等。2016-10-10

