python——itchat模块

本文介绍了如何利用itchat模块给手机助手发送信息和文件,统计微信好友性别比例,结合图灵机器人实现好友聊天,以及通过itchat模块让手机助手管理电脑并执行命令。还提到了os模块的命令执行返回值情况。
部署运行你感兴趣的模型镜像

什么是API?

API:(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节

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

您可能感兴趣的与本文相关的镜像

Python3.11

Python3.11

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值