Python—第三方模块itchat

本文介绍如何利用Python库itchat实现微信自动化操作,包括安装配置、发送消息、统计好友列表及通过微信文件助手执行命令并反馈结果。适用于希望提升工作效率的微信用户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.安装itchat

1.打开pycharm,在settings中安装itchat,过程需要联网

2.添加成功后可以导入itchat模块

 

"""
# _*_coding:utf-8_*_
Name:微信测试.py
Date:1/23/19
Author:westos-dz
"""
"""
联网,运行程序,自动生成二维码,微信二维码登陆,可查看程序效果
"""
import itchat
import time

import random
itchat.auto_login()

# while True:
    #1.给手机助手发送消息
    # itchat.send('hello',toUserName='filehelper')
    # time.sleep(random.randint(1,3))

    #2.统计好友列表
# 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)

##########3################
"""
需求:给文件助手发送消息,执行发送的内容
    1.如果执行成功,显示执行结果
    2.如果执行失败,显示执行失败
    
"""
import os
import itchat
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
    if msg['ToUserName'] == 'filehelper':
        #获取要执行命令的内容
        command = msg['Content']
        print(command)

        #让电脑执行命令代码
        #如果执行成功,返回值为0
        if os.system(command) == 0:
            res = os.popen(command).read()
            result = '【返回值】 - 命令执行成功,执行结果:\n' +res
            itchat.send(result,'filehelper')
        #命令执行失败,请重新输入命令
        else:
            result = '【返回值】 - 命令%s执行失败,请重试' %command
            itchat.send(result,'filehelper')

itchat.auto_login()
itchat.run()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值