API https://itchat.readthedocs.io/zh/latest/api/
教程 https://www.yahboom.com/build.html?id=1999&cid=257
項目地址 https://github.com/revotu/ItChat
更多使用 https://www.cnblogs.com/jiaoyu121/p/6944398.html
重要文件傳輸 : 視頻 圖片 語音 文字
https://www.cnblogs.com/dongxiaodong/p/10490563.html
音樂交互
https://itchat.readthedocs.io/zh/latest/tutorial/tutorial2/

說明: 這玩意相當於網頁版微信,所以登陸了會擠掉電腦端。手機也會提示網頁微信已經登陸,手機通知關閉,但是手機仍然可以接收消息(所以和手機不沖突嘍??)。
1安裝
首先使用pip安裝itchat:
Windows用戶可以直接打開cmd輸入:
pip install itchat
Mac用戶由於沒有對anaconda的python配置環境變量,需要先在終端輸入:
curl https://bootstrap.pypa.io/get-pip.py | python3
樹莓派版本無需下載pip,直接下載itchat輸入:
pip install itchat
pip命令和pip3的區別是:pip3可以直接下載適合python3 使用的package。對於樹莓派,我們可以根據spyder中python的版本使用pip2或pip3下載itchat:
pip2 install itchat
2使用基本例程
首先解決問題,報錯找不到 itchart
https://blog.csdn.net/lockelk/article/details/85296818
可以先查看是否已安裝及安裝的版本
2-1shell下:
pip list
解決:
先查看wxpy的安裝路徑
pip show itchat
然后將路徑添加
python 進入>>>
>>>import sys
>>>sys.path.append("/home/pi/.local/lib/python2.7/site-packages")
其中括號內的為上一步實際查出的安裝路徑。
>>>sys.path 查看已安裝的
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys
sys.path.append("/home/pi/.local/lib/python2.7/site-packages")
import itchat
#登陸 掃描圖片二維碼,一次登陸永久記錄
itchat.auto_login(hotReload=True)
#給文件出書助手,發送一條消息
itchat.send(u'你好', 'filehelper')
#給指定用戶發消息
users=itchat.search_friends("育鈴")
userName= users[0]['UserName']
print(userName)
itchat.send('你好,姚寶貝蛋',toUserName=userName)
#程序不停運行
itchat.run()
2-2 win10下修正錯誤

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys
sys.path.append("F:/dongdong/0tool/python/Anaconda3/Lib/site-packages")
import itchat
itchat.auto_login(hotReload=True)

3 基本使用
3-1 itchat給指定聯系人發消息
https://blog.csdn.net/Lynn_coder/article/details/79436539
import itchat
itchat.auto_login()
itchat.send('Hello, filehelper', toUserName='filehelper')
這段代碼意思是給filehelper發送一個hello,filehelper就是文件助手。
那么我們想給指定的人發消息,並不是把filehelper改掉這么簡單
users=itchat.search_friends("老王")
userName= users[0]['UserName']
print(userName)
itchat.send('你好老王',toUserName=userName)
如果我們想給老王發消息,就先使用itchat.search方法,會把所有備注名為老王的聯系人全都找出來。
之后我們選取第一個(如果你的聯系人列表里只有一個老王,那么就只會搜出來一個)
users[0]取到的是一個聯系人對象,他里面有個key叫UserName,它就是真正的用戶的username
之后我們再使用itchat.send方法,就可以成功向老王發送消息了
完整例程
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys
sys.path.append("F:/dongdong/0tool/python/Anaconda3/Lib/site-packages")
import itchat
#注冊程序打開和關閉函數
def lc():
print('finish login')
itchat.send(u'設備連線', 'filehelper')
def ec():
itchat.send(u'設備離線', 'filehelper')
# 注冊打印出來消息的來源者函數
@itchat.msg_register(itchat.content.TEXT)
def _(msg):
# equals to print(msg['FromUserName'])
print(msg.fromUserName)
#登陸 圖片二維碼掃描 一次登陸之后不用重新掃描
itchat.auto_login(loginCallback=lc, exitCallback=ec,hotReload=True)
#給文件出書助手,發送一條消息
itchat.send(u'你好', 'filehelper')
users=itchat.search_friends("育鈴")
userName= users[0]['UserName']
print(userName)
itchat.send('你好,姚寶貝蛋',toUserName=userName)
#程序不停運行
itchat.run()
#程序停止
#itchat.logout()
發現圖片
這個圖片發送很奇怪,我開始死活發不出去,后來把圖片搞小了一點發出去了,后來再用原來分辨率,又可以了,難道是當時我網速慢???
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys
sys.path.append("F:/dongdong/0tool/python/Anaconda3/Lib/site-packages")
import itchat
#注冊程序打開和關閉函數
def lc():
print('finish login')
itchat.send(u'設備連線', 'filehelper')
def ec():
itchat.send(u'設備離線', 'filehelper')
# 注冊打印出來消息的來源者函數
@itchat.msg_register(itchat.content.TEXT)
def _(msg):
# equals to print(msg['FromUserName'])
print(msg.fromUserName)
#登陸 圖片二維碼掃描 一次登陸之后不用重新掃描
itchat.auto_login(loginCallback=lc, exitCallback=ec,hotReload=True)
#給文件出書助手,發送一條消息
itchat.send(u'你好', 'filehelper')
#找到指定用戶
users=itchat.search_friends("育鈴")#微信備注
userName= users[0]['UserName']
print(userName)
itchat.send('哇,姚寶貝蛋',toUserName=userName)
f="F:/dongdong/1.jpg" #圖片地址
try:
itchat.send_image(f,toUserName=userName) #如果是其他文件可以直接send_file
print("success")
except:
print("fail")
#程序不停運行
itchat.run()
#程序停止
#itchat.logout()
各種消息發送
消息分類:
圖片或表情(PICTURE)、錄制(RECORDING)、附件(ATTACHMENT)、小視頻(VIDEO)、文本(TEXT),地圖(MAP),名片(CARD),通知(NOTE),分享(SHARING),好友邀請(FRIENDS)、語音(RECORDING)、系統消息(SYSTEM)
https://www.cnblogs.com/lizm166/p/9670033.html
可接受監聽的數據類型
圖片或表情(PICTURE)、錄制(RECORDING)、附件(ATTACHMENT)、小視頻(VIDEO)、文本(TEXT),地圖(MAP),名片(CARD),通知(NOTE),分享(SHARING),好友邀請(FRIENDS)、語音(RECORDING)、系統消息(SYSTEM)
控制電腦
https://github.com/BossBobxuan/WeChatToCommand/blob/master/chatToCommand.py
本項目的具體需求是:樹莓派啟動微信服務和OpenCV服務,OpenCV對攝像頭實時視頻監控,當檢測到人臉后后拍照,將拍攝到的照片通過微信發送給用戶的個人好友。
Python3 itchat實現微信定時發送群消息
https://www.cnblogs.com/dongxiaodong/p/10490563.html
查找群失敗的問題
https://www.jianshu.com/p/c8b164b840db
使用 user_info = itchat.search_chatrooms(name='XXX') 查找群的時候,有時會遇到找不到的情況,需要在群設置選擇-保存到通訊錄,才能被查找到
群發助手過年特定發消息 微信作為智能家居入口
https://itchat.readthedocs.io/zh/latest/tutorial/tutorial2/
通過微信獲取城市PM2.5的狀態
https://blog.csdn.net/weixin_33998125/article/details/85077561
自動回復(從Excel讀取配置)
https://www.alphayan.cn/index.php/2018/03/17/itchat_01/
智能機器人
https://itchat.readthedocs.io/zh/latest/tutorial/tutorial0/#_5
詞雲圖
https://blog.csdn.net/xiaoxianerqq/article/details/81428054

全國好友分布地址
https://blog.csdn.net/xiaoxianerqq/article/details/81428054

二、py庫
1,itchat:這個是主要的工具,用於連接微信個人賬號接口。以下是一些相關的知識點網站。
2,xlrd:這個是用來讀Excel文件的工具。
3,apscheduler:這個是用來定時調度時間的工具。
本項目中,對於微信的操作,我們需要用到的API是itchat。 itchat是一個微信對於python封裝的api,它的功能非常強大,我們可以使用它和python對自己微信的很多信息進行爬取。

綜合示例
######################################### 程序說明 ########################################
# 功能:
# 集合了接收消息處理,主動發送消息處理
# 使用:
# 1運行后,第一次掃描二維碼登陸,可以圖片形式,也可以命令行形式顯示二維碼
# 2注冊接收消息和文件處理函數
# 2-1 個人用戶或者群給自己發的消息,圖片,文件,視頻,保存等機制
# 2-2 自己通過文件助手給自己發的消息
# 3主動給別人發消息
# 3-1 自己給普通用戶或者群發消息,各種文件,圖片,視頻
# 3-2 自己給文件助手發送消息,相當於給自己發消息,用來控制設備,發送文件給設備等
#
###########################################################################################
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys
sys.path.append("F:/dongdong/0tool/python/Anaconda3/Lib/site-packages")
import itchat
#各類型消息的注冊
from itchat.content import *
from datetime import datetime
#from itchat.content import PICTURE, RECORDING, ATTACHMENT, VIDEO,TEXT
######################################### 消息發送函數 ########################################
#1-1注冊程序打開和關閉函數
def lc():
print('finish login')
itchat.send(u'設備連線', 'filehelper')
def ec():
itchat.send(u'設備離線', 'filehelper')
#1-2給普通用戶發送圖片
def send_pic(picpath,toUserName):
try:
itchat.send_image(picpath,toUserName) #第一種通用發送方式 如果是其他文件可以直接send_file
#itchat.send('@img@%s' % picpath, toUserName) #第二種指定類型發送方式
print("success_jpg")
except:
print("fail_jpg")
#1-3給普通用戶發送文件
def send_file(filepath,toUserName):
try:
#itchat.send('@fil@%s' % './filex/tt.txt', toUserName=msg['FromUserName'])
itchat.send_file(filepath, toUserName) # txt如果是空的就不會發送
print("success_file")
except:
print("fail_file")
#1-4給普通用戶發送視頻
def send_video(videopath,toUserName):
try:
#itchat.send('@vid@%s' % './filex/videox.mp4', toUserName=msg['FromUserName'])
itchat.send_video(videopath, toUserName)
print("success_video")
except:
print("fail_video")
#1-5給群發消息
def SentChatRoomsMsg(chatroomName ,context):
itchat.get_chatrooms(update=True)
chatrooms = itchat.search_chatrooms(name=chatroomName)
#有時會遇到找不到的情況,需要在該群設置選擇-保存到通訊錄,才能被查找到
if len(chatrooms)==0 :
print('沒有找到群聊:' + chatroomName)
# exit(0)
else:
print(chatrooms[0]['UserName'])#輸出群聊標識符
itchat.send_msg(context, toUserName=chatrooms[0]['UserName'])#發送消息
######################################### 消息處理回調函數 ########################################
# 消息分類:
# 圖片或表情(PICTURE)、錄制(RECORDING)、附件(ATTACHMENT)、小視頻(VIDEO)、文本(TEXT)
# 地圖(MAP),名片(CARD),通知(NOTE),分享(SHARING),好友邀請(FRIENDS)、語音(RECORDING)、系統消息(SYSTEM)
# 1-3 文本消息接收和處理
# 1自己發給文件助手的
# 2別人發給己的
#@itchat.msg_register(itchat.content.TEXT,isGroupChat=True) # 2-1只針對群消息
#@itchat.msg_register(itchat.content.TEXT) # 2-2自針對普通用戶消息
@itchat.msg_register(itchat.content.TEXT, isFriendChat=True, isGroupChat=True, isMpChat=True) # 2-3針對所有人消息
def text_reply(msg):
#if msg['ToUserName'] != 'filehelper': return # 過濾掉不是文件助手的消息
#print(msg) # 打印消息 所以的信息
#print(msg["Text"]) # 收到的消息是什么
#print(msg["ToUserName"]) # 發給誰的
rebacktxt_msg= "收到:"+msg["Text"]
#判斷是否是@本人
#if msg["Text"].find(usermsgx["RemarkName"])== '育鈴':
if msg["ToUserName"].find("filehelper")==0: # 如果是給文件助手發的
itchat.send(rebacktxt_msg, 'filehelper')
return rebacktxt_msg # 回復除了自己以外人的消息
#1-4文件下載---存在同級filex目錄下
#1別人發給自己的消息
#2自己發給文件助手的
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
filedpx="./filex/"+msg["FileName"] # 得到文件路徑+名字 目錄需要手動創建
msg.download(filedpx)
rebackfile_msg="收到文件類型"+msg['Type']+" 保存地址為:filex/"+msg.fileName
if msg["ToUserName"].find("filehelper")==0: # 1如果是給文件助手發的,發個提醒
itchat.send(rebackfile_msg, 'filehelper')
return rebackfile_msg # 2如果是別人發給自己的,直接回復對方
######################################### 主函數函數 ########################################
#1登陸
itchat.auto_login(hotReload=True,loginCallback=lc, exitCallback=ec) #圖片二維碼掃描 登陸之后一段時間內不用重新掃描,注冊打開和結束回調函數
#2-1找到自己的名字
usermsgx=itchat.search_friends() #得到當前用戶的信息 首位是自己
#print(usermsgx) #我的微信ID
#print(usermsgx["NickName"]) #我的備注名字
#2-2找到昵稱為“”的用戶
users=itchat.search_friends("育鈴") #按照昵稱查找
#itchat.search_friends(userName='@abcdefg1234567') #按照ID查找
userName= users[0]['UserName']
#print(userName)
#3-1-1給文件助手,發送一條消息
itchat.send(u'你好文件助手,這是一條文本測試消息', 'filehelper')
#3-1-2給個人用戶,發送一條消息
#itchat.send('東小東: 這是一個來自樹莓派的測試',toUserName=userName)
#3-3給個人用戶, 發送圖片
pic_file_name='./filex/1.txt' #圖片相對路徑地址
#send_pic("F:/dongdong/1.jpg",'filehelper') #圖片絕對路徑地址
#3-4發送文件
#send_file('./filex/1.txt','filehelper')
#3-5發送視頻
#send_video('./filex/1.mp4','filehelper')
#3-6群名向群里發送內容
#SentChatRoomsMsg('溫縣獨霸', '你好')
#程序保持運行
itchat.run()
#程序停止
#itchat.logout()
