Python利用itchat庫向好友或者公眾號發消息


首先獲得好友或者公眾號的UserName 
1.獲取好友UserName

#coding=utf8 import itchat itchat.auto_login(hotReload=True) #想給誰發信息,先查找到這個朋友,name后填微信備注即可,deepin測試成功 users = itchat.search_friends(name='') #獲取好友全部信息,返回一個列表,列表內是一個字典 print(users) #獲取`UserName`,用於發送消息 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.獲取公眾號UserName

#coding=utf8 import itchat itchat.auto_login(hotReload=True) #返回完整的公眾號列表 mps = itchat.get_mps() ## 獲取名字中含有特定字符的公眾號,也就是按公眾號名稱查找,返回值為一個字典的列表 mps = itchat.search_mps(name='CSDN') print(mps) #發送方法和上面一樣 userName = mps[0]['UserName'] itchat.send("hello",toUserName = userName) 

 

3.發送內容代碼如下

#coding=utf8 import itchat itchat.auto_login(hotReload=True) #獲取通訊錄信息 account=itchat.get_friends() # #獲取自己的UserName userName = account[0]['UserName'] #獲取公眾號信息 # mps = itchat.get_mps() # print(mps) lines = [] #讀取txt文件 f = open("/home/numb/Desktop/aaa.txt") lines = f.readlines()#讀取全部內容 #循環發送文本內容 for i in range(90): #UserName需要用上面獲取的自己修改 itchat.send(lines[i],toUserName='UserName') print("Success")

參考:http://blog.csdn.net/th_num/article/details/55657453


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM