用python發(fā)送微信消息
更新時間:2020年12月21日 11:48:11 作者:冰渣渣
這篇文章主要介紹了用python發(fā)送微信消息的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
條件
1、能夠上網(wǎng)
2、必須是你的好友
3、必須能二維碼登錄網(wǎng)頁微信
發(fā)送示例
# 使用微信接口給微信好友發(fā)送消息, import itchat # 自動登錄方法,hotReload=True可以緩存,不用每次都登錄,但是第一次執(zhí)行時會出現(xiàn)一個二維碼,需要手機微信掃碼登錄 itchat.auto_login(hotReload=False) # 搜索好友,search_friends("xxx"),其中"xxx"為好友昵稱,備注或微信號不行 userfinfo = itchat.search_friends("顧正") # "智能群管家014"為好友昵稱 print("userfinfo:",userfinfo) # print(userfinfo),獲取userinfo中的UserName參數(shù) userid = userfinfo[0]["UserName"] # 獲取用戶id # 調(diào)用微信接口發(fā)送消息 itchat.send("陳軍是不是傻?", userid) # 通過用戶id發(fā)送信息 # 或 itchat.send_msg(msg='好像是的', toUserName=userid) # 發(fā)送純文本信息
定時發(fā)送消息
# -*- 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] #因為會出現(xiàn)進程報錯,所以我加上了 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天發(fā)送消息 t=time(86400,send_news()) t.start() except: #如果上面其中一條消息沒有發(fā)送成功,就會發(fā)送本條消息 message4=u"你的朋友出bug了" itchat.send(message4,toUserName=FriendName) if __name__=="__main__": send_news()
以上就是用python發(fā)送微信消息的詳細內(nèi)容,更多關(guān)于python 發(fā)送微信消息的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
淺析python實現(xiàn)布隆過濾器及Redis中的緩存穿透原理
本文帶你了解了位圖的實現(xiàn),布隆過濾器的原理及 Python 中的使用,以及布隆過濾器如何應(yīng)對 Redis 中的緩存穿透,相信你對布隆過濾器已經(jīng)有了一定的認識2021-09-09Python使用win32com實現(xiàn)的模擬瀏覽器功能示例
這篇文章主要介紹了Python使用win32com實現(xiàn)的模擬瀏覽器功能,結(jié)合實例形式分析了Python基于win32com模塊實現(xiàn)網(wǎng)頁的打開、登陸、加載等功能相關(guān)技巧,需要的朋友可以參考下2017-07-07解決tensorflow模型壓縮的問題_踩坑無數(shù),總算搞定
這篇文章主要介紹了解決tensorflow模型壓縮的問題_踩坑無數(shù),總算搞定!希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03Python 如何實時向文件寫入數(shù)據(jù)(附代碼)
這篇文章主要介紹了Python 如何實時向文件寫入數(shù)據(jù)(附代碼),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07