zoukankan      html  css  js  c++  java
  • 用Python玩转微信

     

    Python玩转微信

    大家每天都在用微信,有没有想过用python来控制我们的微信,不多说,直接上干货!  这个是在 itchat上做的封装  http://itchat.readthedocs.io/zh/latest/

    安装模块

    pip3  install  wxpy    
    
    pip install  wxpy -i "https://pypi.doubanio.com/simple/"   #豆瓣源

    1.生成微信对象

    bot = Bot()   #初始化一个对象,就相当于拿到了这个人的微信,后续的一些操作都要用它来完成
    bot = Bot(cache_path=True)  #缓存登陆状态

    2.分别找到微信对象的好友,聊天对象,朋友,群组,公众号

    friends = bot.friends()  # 获取朋友            chats = bot.chats()      # 获取聊天对象
    groups = bot.groups()    #获取群聊
    maps = bot.maps()        #获取公众号
    
    # 拿到的都是列表  如果要取到对象加上角标[0]
    但是这样很麻烦
    推荐方法,这样写
    ensure_one(bot.groups().search('全栈开发脱产11期'))

    3.  查找某个好友

    friend = bot.friends().search('袁勇')[0]

     4.向好友发送消息

    # 发送文本
    my_friend.send('Hello, WeChat!')
    # 发送图片
    my_friend.send_image('my_picture.png')
    # 发送视频
    my_friend.send_video('my_video.mov')
    # 发送文件
    my_friend.send_file('my_file.zip')
    # 以动态的方式发送图片
    my_friend.send('@img@my_picture.png')

    5.统计微信好友的信息,比如男女比例,地域分配,等等

    bot.friends().stats_text()

     6.监听群里面某个人的消息

    from wxpy import *
    
    bot = Bot()
    # 定位公司群
    company_group = ensure_one(bot.groups().search('公司微信群'))
    
    # 定位老板
    boss = ensure_one(company_group.search('老板大名'))
    
    # 将老板的消息转发到文件传输助手
    @bot.register(company_group)
    def forward_boss_message(msg):
         if msg.member == boss:
             msg.forward(bot.file_helper, prefix='老板发言')
    
    # 堵塞线程
    embed()

    7.接入图灵机器人  让机器人来回复好友信息

    from wxpy import *
    import wxpy
    from wxpy import *
    bot = Bot()   #初始化一个对象,就相当于拿到了这个人的微信,后续的一些操作都要用它来完成
    # me = ensure_one(bot.search('袁勇'))
    # me.send('哈哈')
    all_friends = bot.friends()  # 找到我所有的好友
    tuling = Tuling(api_key='0f329eba0af742cfb34daa64f9edef8b') # 接入图灵机器人
    for friend in all_friends :
        @bot.register(friend)
        def reply_me_friend(msg):
            tuling.do_reply(msg)
    embed()

    8.设置最大保存信息条数,并且可以搜索

    bot = Bot()
    # 设置历史消息的最大保存数量为 10000 条
    bot.messages.max_history = 10000
    
    # 搜索所有自己发送的,文本中包含 'wxpy' 的消息
    bot.messages.search('wxpy', sender=bot.self)

     9.用微信监控你的程序

          1.获得专用logger

    wxpy.get_wechat_logger(receiver=None, name=None, level=30)
    获得一个可向指定微信聊天对象发送日志的 Logger
    
    参数:
    receiver –
    当为 None, True 或字符串时,将以该值作为 cache_path 参数启动一个新的机器人,并发送到该机器人的”文件传输助手”
    当为 机器人 时,将发送到该机器人的”文件传输助手”
    当为 聊天对象 时,将发送到该聊天对象
    name – Logger 名称
    level – Logger 等级,默认为 logging.WARNING
    返回:
    Logger

        2.指定一个群为消息接受者

    from wxpy import *
    
    # 初始化机器人
    bot = Bot()
    # 找到需要接收日志的群 -- `ensure_one()` 用于确保找到的结果是唯一的,避免发错地方
    group_receiver = ensure_one(bot.groups().search('XX业务-告警通知'))
    
    # 指定这个群为接收者
    logger = get_wechat_logger(group_receiver)
    
    logger.error('打扰大家了,但这是一条重要的错误日志...')   #默认的日志级别设置为WARNING(日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG)

    3.将异常消息发送到指定对象那里

    from wxpy import get_wechat_logger
    
    # 获得一个专用 Logger
    # 当不设置 `receiver` 时,会将日志发送到随后扫码登陆的微信的"文件传输助手"
    logger = get_wechat_logger()
    
    #指定接受对象
    group_reciver = ensure_one(bot.groups().search('全栈开发脱产11期'))
    
    # 发送警告
    logger.warning('这是一条 WARNING 等级的日志,你收到了吗?')
    
    # 接收捕获的异常
    try:
        1 / 0
    except Exception as e
        logger.exception(e)

    关注公众号:Python爬虫数据分析挖掘,学习更多python知识

    耐得住寂寞,才能登得顶
    Gitee码云:https://gitee.com/lyc96/projects
  • 相关阅读:
    Oracle 按一行里某个字段里的值分割成多行进行展示
    Property or method "openPageOffice" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by
    SpringBoot 项目启动 Failed to convert value of type 'java.lang.String' to required type 'cn.com.goldenwater.dcproj.dao.TacPageOfficePblmListDao';
    Maven 设置阿里镜像
    JS 日期格式化,留作参考
    JS 过滤数组里对象的某个属性
    原生JS实现简单富文本编辑器2
    Chrome控制台使用详解
    android权限(permission)大全
    不借助第三方网站四步实现手机网站转安卓APP
  • 原文地址:https://www.cnblogs.com/chenlove/p/14038668.html
Copyright © 2011-2022 走看看