用python傳送微信訊息
阿新 • • 發佈:2020-12-22
條件
1、能夠上網
2、必須是你的好友
3、必須能二維碼登入網頁微信
傳送示例
# 使用微信介面給微信好友傳送訊息, import itchat # 自動登入方法,hotReload=True可以快取,不用每次都登入,但是第一次執行時會出現一個二維碼,需要手機微信掃碼登入 itchat.auto_login(hotReload=False) # 搜尋好友,search_friends("xxx"),其中"xxx"為好友暱稱,備註或微訊號不行 userfinfo = itchat.search_friends("顧正") # "智慧群管家014"為好友暱稱 print("userfinfo:",userfinfo) # print(userfinfo),獲取userinfo中的UserName引數 userid = userfinfo[0]["UserName"] # 獲取使用者id # 呼叫微信介面傳送訊息 itchat.send("陳軍是不是傻?",userid) # 通過使用者id傳送資訊 # 或 itchat.send_msg(msg='好像是的',toUserName=userid) # 傳送純文字資訊
定時傳送訊息
# -*- coding: UTF-8 -*- import itchat import time import requests #獲取金山詞霸每日一句 def get_new(): url="http://open.iciba.com/dsapi" r=requests.get(url) contents=r.json()['content'] note=r.json()['note'] return contents,note def send_news(): try: itchat.auto_login(hotReload=True) my_friend=itchat.search_friends(name=u'卡2') FriendName=my_friend[0]["UserName"] message1=get_new()[0] #因為會出現程序報錯,所以我加上了 pass pass message2=get_new[1] pass message3=u"來自你的朋友" pass itchat.send(message1,toUserName=FriendName) itchat.send(message2,toUserName=FriendName) itchat.send(message3,toUserName=FriendName) #每個1天傳送訊息 t=time(86400,send_news()) t.start() except: #如果上面其中一條訊息沒有傳送成功,就會發送本條訊息 message4=u"你的朋友出bug了" itchat.send(message4,toUserName=FriendName) if __name__=="__main__": send_news()
以上就是用python傳送微信訊息的詳細內容,更多關於python 傳送微信訊息的資料請關注我們其它相關文章!