1.利用itchat模块给手机助手发信息及文件
import itchat
import time
hotReload = True ##保留登录登录状态,段时间内不会自动登录
itchat.auto_login()
while True:
itchat.send('hello',toUserName='filehelper') ##对手机助手发送消息
# itchat.send_file('/etc/passwd', toUserName='filehelper') ##对手机助手发送文件
time.sleep(3) ##每3秒发送一次
执行命令,出现二维码,扫码登录,微信文件助手会出现发送来的短信及文件
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 『被活捉的萌懒猫』 ##登录成功
2.统计微信里的男女好友比例
import itchat
import time
hotReload = True
itchat.auto_login()
friends = itchat.get_friends() ##获取好友列表
print(friends)
info = {}
for friend in friends[1:]:
if friend['Sex'] == 1:
info['male'] = info.get('male',0)+1
elif friend['Sex'] == 2:
info['female'] = info.get('female',0)+1
else:
info['other'] = info.get('other',0)+1
print(info)
结果输出
[<User: {'MemberList': <ContactList: []>, 'UserName': '@c24396ce2572d34fe209cf822080d71b0e5d726bf1806a45398c19d8840aa693', 'City': '', 'DisplayName': '', 'PYQuanPin': '', 'RemarkPYInitial': '',......]
{'male': 118, 'other': 8, 'female': 67}
3.利用图灵机器人进行好友聊天
先创建自己的机器人
import itchat
import requests
def get_tuling_reponse(_info):
api_url = 'http://www.tuling123.com/openapi/api' ##指定的api地址
data = {
'key':'03782273ff0a4593ae80dd0e786551fb', ##api端口接入的key
'info':_info,
'userid':'girl' ##对应图灵机器人的姓名
}
res = requests.post(api_url,data).json() ##发送数据到指定的网址,获取网址返回的数据
print(res['text'])
return(res['text'])
@itchat.msg_register(itchat.content.TEXT,isFriendChat=True) ##仅获取好友的文本信息,并且给与恢复
def text_repky(msg): ##获取好友发送的信息
content = msg['Content'] ##返回文本信息内容
returnContent = get_tuling_reponse(content) ## 将好友的消息发送给机器人去处理,处理的结果就是返回给好友的信息
return returnContent
itchat.auto_login()
itchat.run()
结果测试:
你真棒
必须棒
哈哈哈
要和我分享好玩儿的吗?
你真漂亮
亲爱的,当天请求次数已用完。
4.利用itchat模块可以使手机助手管理电脑并且执行命令
import os
import itchat
@itchat.msg_register(itchat.content.TEXT,isFriendChat=True)
def text_reply(msg):
if msg[‘ToUserName’]==‘filehelper’:
command = msg[‘Content’] ##获取要执行的命令
if os.system(command)==0: ##电脑执行命令代码时,执行成功会返回0
res = os.popen(command).read()
result = ‘[返回值]-命令执行成功,执行结果为:\n’+res
itchat.send(result,‘filehelper’)
else:
result = ‘[返回值]-命令执行失败,请重测’ %(command)
itchat.send(result,‘filehelper’)
itchat.auto_login()
itchat.run()
结果测试:
/home/kiosk/PycharmProjects/0529 ##微信执行pwd命令
01.py ##微信执行ls命令
02.py
03.py
04.py
different.html
diff.html
difflib模块.py
groupDiff.html
itchat 模块.py
__pycache__
异常.py
时间模块.py
考题.py
装饰器2.py
装饰器.py
验证码.py
高阶函数.py
高阶函数练习.py
5.os模块的补充
import os
print(os.system('pwd'))
res = os.system('hostnamee')
print(res)
/home/kiosk/PycharmProjects/0529
0
32512
sh: hostnamee: command not found
由此可知,命令执行成功时返回值为0;不成功时,返回值不为0
res = os.popen('hostname').read() ##用于保存命令执行的结果
print(res)
foundation4.ilt.example.com