• 微信自动回复


    微信自动回复

    下载安装 pip install itchat

    微信登录获取好友列表

    1.登录微信

    2.获取微信好友列表

    import itchat
    #登录微信
    itchat.login()
    #获取好友列表
    friends=itchat.get_friends()
    for i in friends:
       print()

    获取微信好友头像

    1.登录微信

    2.获取好友列表

    3.获取微信好友头像

    4.设置保存头像路径

    5.保存头像

    6.程序跑起来

    import itchat
    #登录微信
    itchat.auto_login()
    #获取微信好友
    for i in itchat.get_friends()
        print(i['RemarkName'])
    #获取微信好友头像
        img=itchat.img_head_img(Usernsme=i[Username])
    #设置保存路径,并以好友备注存起来
        path='保存头像的路径文件夹'+i['NickName'].jpg
    #保存头像
        with open(path,'wb') as f:
            f.write(img)
     itchat.run()

    获取微信好友男女比例

    1.登录微信

    2.获取好友

    3.设置变量男、女,未知都为0

    4.用if获取微信男女好友

    5.用所统计的男、女、未知%总数*100=比例 

    import itchat
    #登录微信,,,,,括号里面只登陆一次
    itchat.auto_login(hotReload=True)
    #设置变量
    male=female=other=0
    #获取好友
    friends=itchat.get_friends()
    #判断男女好友
    for i in friends[0]:
        if i['Sex']==1:
            male+=1
        elif i['Sex']==2:
            female+=1
        else:
            other+=1
    #打出总共好友
    total=len(friends)
    print(total)
    #用算法求出比例
    male_proportion=male%total*100
    female_proportion=female%total*100
    other_proportion=other%total*100
    print('男生比例%s'%male_proportion)
    print('女生比例%s'%female_proportion)
    print('未知%s'%other_proportion)

    微信接收发送消息

    这里需要用的是图灵接口

    有网址百度图灵网址http://www.turingapi.com/

    1.登录微信

    2.设置path图灵网站

    3.用函数写个接收消息

    4.根据好友发的消息回复相关信息

    import itchat
    import requests
    #登录微信
    itchat.auto_login(hotReload=True)
    #放入图灵使用网址
    path='http://www.tuling123.com/openapi/api'
    #定义一个接收消息的函数,传参
    def message(message):
        date={
            #这个是机器人的使用数据
            'key':'37515ec824ab4f059c7895c8f7e6c13e',
            #输出的参数
            'info':message,
            #机器人使用
            'userId':'robot'
        }
        #异常处理
        try:
            #用post形式接收
            r=requests.post(path,date=date).json()
            #返回消息
            print('回复%s'%r['text'])
            return r['text']
        except:
            #如果错了就返回空
            return  ''
    #装饰器,使用其中方法
    @itchat.msg_register(itchat.content.TEXT)
    #定义发送消息
    def reply(msg):
        #随便定个变量也可以不要
        delattrs='明白'
        #这里是这只机器人回复的对象
        friend=itchat.search_friends('你的回复对象')
        #提取朋友
        readfriend=friend[0]
        #执行接收的的消息
        replys=message(msg('Text'))
        #判断如果是设置的人发的消息机器人自动回复
        if msg['FromUserName']==readfriend:
            itchat.send(delattrs or replys,toUserName=readfriend)
    #进行程序跑起来
    itchat.run()
    reply()
    View Code

     

  • 相关阅读:
    TF400916错误修复办法
    架构优化
    .net 动态代理的泛型方法支持问题
    解决Kafka-1194问题
    SOA服务总线设计
    SOA初探
    重构心法——提高代码复用率
    重构心法——拆分大对象
    重构心法——你应该这样写方法
    反爬虫那些事儿
  • 原文地址:https://www.cnblogs.com/cui00/p/12692439.html
Copyright © 2020-2023  润新知