新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Python如何使用itchat庫向好友或者公眾號(hào)發(fā)消息-創(chuàng)新互聯(lián)
這篇文章主要為大家展示了“Python如何使用itchat庫向好友或者公眾號(hào)發(fā)消息”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Python如何使用itchat庫向好友或者公眾號(hào)發(fā)消息”這篇文章吧。
首先獲得好友或者公眾號(hào)的UserName
1. 獲取好友UserName
#coding=utf8 import itchat itchat.auto_login(hotReload=True) #想給誰發(fā)信息,先查找到這個(gè)朋友,name后填微信備注即可,deepin測(cè)試成功 users = itchat.search_friends(name='') #獲取好友全部信息,返回一個(gè)列表,列表內(nèi)是一個(gè)字典 print(users) #獲取`UserName`,用于發(fā)送消息 userName = users[0]['UserName'] itcha.send("hello",toUserName = userName)
#coding=utf8 import itchat itchat.auto_login(hotReload=True) #獲取所有好友信息 account=itchat.get_friends() # #獲取自己的UserName userName = account[0]['UserName']
2. 獲取公眾號(hào)UserName
#coding=utf8 import itchat itchat.auto_login(hotReload=True) #返回完整的公眾號(hào)列表 mps = itchat.get_mps() ## 獲取名字中含有特定字符的公眾號(hào),也就是按公眾號(hào)名稱查找,返回值為一個(gè)字典的列表 mps = itchat.search_mps(name='CSDN') print(mps) #發(fā)送方法和上面一樣 userName = mps[0]['UserName'] itchat.send("hello",toUserName = userName)
3. 發(fā)送內(nèi)容代碼如下
#coding=utf8 import itchat itchat.auto_login(hotReload=True) #獲取通訊錄信息 account=itchat.get_friends() # #獲取自己的UserName userName = account[0]['UserName'] #獲取公眾號(hào)信息 # mps = itchat.get_mps() # print(mps) lines = [] #讀取txt文件 f = open("/home/numb/Desktop/aaa.txt") lines = f.readlines()#讀取全部?jī)?nèi)容 #循環(huán)發(fā)送文本內(nèi)容 for i in range(90): #UserName需要用上面獲取的自己修改 itchat.send(lines[i],toUserName='UserName') print("Success")
以上是“Python如何使用itchat庫向好友或者公眾號(hào)發(fā)消息”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章題目:Python如何使用itchat庫向好友或者公眾號(hào)發(fā)消息-創(chuàng)新互聯(lián)
鏈接地址:http://www.dlmjj.cn/article/deipho.html