python微信相关应用

内容概要: itchat安装, 登录, 获取个人信息

1. itchat安装

1
sudo pip3 install itchat

2. 登录并获取个人信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import itchat
from time import sleep
from itchat.content import *

@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
# print(msg['FromUserName'],'------',msg.text)
# print(msg.text+" from 张杰")
return msg.text

@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
def text_reply(msg):
msg.user.send('%s: %s' % (msg.type, msg.text))

@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
msg.download(msg.fileName)
typeSymbol = {
PICTURE: 'img',
VIDEO: 'vid',
}.get(msg.type, 'fil')
return '@%s@%s' % (typeSymbol, msg.fileName)

@itchat.msg_register(FRIENDS)
def add_friend(msg):
msg.user.verify()
msg.user.send('Nice to meet you!')

@itchat.msg_register(TEXT, isGroupChat=True)
def text_reply(msg):
if msg.isAt:
msg.user.send(u'@%s\u2005I received: %s' % (
msg.actualNickName, msg.text
))



# itchat.auto_login(enableCmdQR=True)
itchat.auto_login(hotReload=True)
friends = itchat.get_friends()
print(friends[0])

for i,v in friends[0].items():
print(i," : ",v)

itchat.run()


def getFriends():
count = 0
for user in friends:
count+=1
print('NO.',count,'--->>>',user['NickName']," ------>>>> ",user['Signature'])
msg = 'NO.'+str(count)+'--->>>'+user['NickName']+"----->>>>"+user['Signature']
sleep(1)
itchat.send(msg,toUserName='@2a2337d073abf4ffd31fefdba94b7f3bd9814d53a9221a78696abfec0fb0f127')
# itchat.send(msg,toUserName='filehelper')

运行结果

1
2
3
4
5
6
7
8
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.
TERM environment variable not set.
Login successfully as Jay
...