itchat模块的使用

itchat模块的使用

pip install itchat

# -*-coding:utf-8-*-
import itchat
import itchat.content as itcontent
# 登录网页微信,hotReload=True 能让登录时间加长
itchat.auto_login()
#itchat.auto_login(hotReload=True)
Getting uuid of QR code.
Downloading QR code.
Please scan the QR code to log in.
Please press confirm on your phone.
Loading the contact, this may take a little while.
Login successfully as 岳婷
# 记录公众号机器人小冰的UserName
mps = itchat.search_mps(name='小冰')
AI = mps[0]['UserName']
print(AI)
# 记录自己的UserName,不然发送消息会发两遍
username = itchat.get_friends()
user = username[0]['UserName']
#print(username)
# print(username)
for i in range(0,1491):#1491:好友人数
    NickName = username[i]['NickName']
    Signature = username[i]['Signature']
    Sex = username[i]['Sex']
    Province = username[i]['Province']
    print(NickName,Signature,Sex,Province)#打印微信好友的昵称、签名、性别和省份
# 记录好友列表里好友的 UserName
friendsname = [friend['UserName'] for friend in username if friend['UserName'] != user]
#print(friendsname)
groupname = itchat.get_chatrooms()
groups = [group['UserName'] for group in groupname]
#print(groupname[0])
for i in range(0,13):
    NickName = groupname[i]['NickName']
    MemberCount = groupname[i]['MemberCount']
    print(NickName,MemberCount)#打印微信群的名字、群人数(只有保存在通讯录的群可以被找到)

实现自动回复需要itchat.run()
给好友发送消息:

import itchat
itchat.auto_login()
█

Getting uuid of QR code.
Downloading QR code.
Please scan the QR code to log in.
Please press confirm on your phone.
Loading the contact, this may take a little while.
Login successfully as 岳婷
<ItchatReturnValue: {'BaseResponse': {'Ret': 0, 'ErrMsg': '请求成功', 'RawMsg': '请求成功'}, 'MsgID': '3473816735185157982', 'LocalID': '15511548792221'}>
users=itchat.search_friends("王")
userName= users[0]['UserName']
#print(userName)
itchat.send('你好小王',toUserName=userName)
<ItchatReturnValue: {'BaseResponse': {'Ret': 0, 'ErrMsg': '请求成功', 'RawMsg': '请求成功'}, 'MsgID': '8460422035919031145', 'LocalID': '15511549953620'}>
from itchat.content import *
@itchat.msg_register([PICTURE,TEXT])
def simple_reply(msg):
    if msg['Type'] == TEXT:
        ReplyContent = 'I received message: '+msg['Content']
    if msg['Type'] == PICTURE:
        ReplyContent = 'I received picture: '+msg['FileName']
    itchat.send_msg(ReplyContent,msg['FromUserName'])
itchat.run()
Start auto replying.
LOG OUT!