zoukankan      html  css  js  c++  java
  • 微信机器人

    用Python对你的微信好友进行数据分析,第一次用类的方法来做,接下来直接上代码

    from wxpy import Bot
    import openpyxl
    from pyecharts import Map
    import wordcloud
    import matplotlib.pyplot as plt
    import jieba
    class WeChatRobot:
        def __init__(self,toexcel="",tocity="",tosfditu=""):
            self.bot=Bot()
            self.allfriends_info=self.bot.friends()
            self.allfriends_num=len(self.allfriends_info)
            self.excelwj=toexcel
            self.wcofcitywj=tocity
            self.shengfenwj=tosfditu
            self.run()
        def getfriends(self):
            self.friendsinfo=[]
            self.infotitle=['nick','beizhu','sex','province','city']
            for afriend in self.allfriends_info:
                nick=afriend.raw.get(self.infotitle[0],None)
                beizhu=afriend.raw.get(self.infotitle[1],None)
                sex={1:"",2:"",0:"其他"}.get(afriend.raw.get(self.infotitle[2],None),None)
                province=afriend.raw.get(self.infotitle[3],None)
                city=afriend.raw.get(self.infotitle[4],None)
                listmp=[nick,beizhu,sex,province,city]
                self.friendsinfo.append(listmp)
        def bcexcel(self,excelname):
            workbook=openpyxl.Workbook()
            sheet=workbook.active
            sheet.title='weixinhaoyou'
            for _ in range(len(self.infotitle)):
                sheet.cell(row=1,column=_+1,value=self.infotitle[_])
            for i in range(self.allfriends_num):
                for j in range(len(self.infotitle)):
                    sheet.cell(row=i+2,column=j+1,value=str(self.friendsinfo[i][j]))
            if excelname!="":
                workbook.save(excelname)
                print("chenggong")
        def fenxi(self):
            print(self.allfriends_info.stats_text())
        def cityciyun(self,cityname):
            citystr=""
            for i in range(self.allfriends_num):
                if self.friendsinfo[i][4] not in citystr:
                    citystr +=" "+self.friendsinfo[i][4]
                    wordlist=jieba.lcut(citystr)
                    citystr=' '.join(wordlist)
                    font='simhei.ttf'
                    wc=WordCloud(
                        background_color="white",
                        width=1300,
                        height=620,
                        max_font_size=200,
                        random_state=200,
                        )
            myword=wc.generate(citystr)
            plt.imshow(myword)
            plt.axis('off')
            plt.show()
            if cityname!="":
                wc.to_file(cityname)
                print("chenggong")
        def scsfdt(self,mapfile):
            sflist=[]
            sfnum=[]
            for i in range(self.allfriends_num):
                if self.friendsinfo[i][3] not in sflist:
                    sflist.append(self.friendsinfo[i][3])
                    sfnum.append(0)
            for i in range(self.allfriends_num):
                for j in range(lrn(sflist)):
                    if self.friendsinfo[i][3]==sflist[j]:
                        sfnum[j]+=1
            map=Map("haoyoufenbu",width=100,heigth=800)
            map.add("",sflist,sfnum,maptype="china",is_vlauemap=True,visual_text_color='#000')
            if mapfile!="":
                map.render(mapfile)
                print("chenggong")def run(self):
            self.getfriends()
            print("get!")
            print("members",self.allfriends_num)
            self.bcexcel(self.excelwj)
            self.fenxi()
            self.cityciyun(self.wcofcitywj)
            self.scsfdt(self.shengfenwj)
            self.sccsdt(self.chengshiwj)
    if __name__=="__main__":
        toexcel="D:\python文件\haoyoushuju.xlsx"
        totupian="D:\python文件\citywc.png"
        toshengfen="D:\python文件\sfditu.html"
        robot=WeChatRobot(toexcel,totupian,toshengfen)

    运行结果如下:

    接下来是自动回复机器人的代码

    from wxpy import *
    import requests
    import json
    bot=Bot()
    robot = Bot()
    def talk_robot(info='你好啊'):   #定义一个默认参数
        api_url = 'http://www.tuling123.com/openapi/api'  # 图灵接口url
        apikey =      # 图灵密钥 
        data = {'key': apikey, 'info': info}                                   
        r = requests.post(api_url, data=data).text
        response = json.loads(r)['text']
        return response
    #在群里艾特你自动回复
    @robot.register()
    def reply_my_friend(msg):
        message = '{}'.format(msg.text)
        response = talk_robot(info=message)
        return response
    embed()
  • 相关阅读:
    Django的模型层(1)- 单表操作(上)
    Django-1版本的路由层、Django的视图层和模板层
    Django准备知识-web应用、http协议、web框架、Django简介
    MySQL数据库(5)- pymysql的使用、索引
    MySQL数据库(4)- 多表查询、可视化工具Navicat的使用、设计模式MVC
    MySQL练习题
    MySQL数据库(3)- 完整性约束、外键的变种、单表查询
    MySQL数据库(2)- 库的操作、表的操作、数据的操作、存储引擎的介绍
    MySQL数据库(1)- 数据库概述、MySQL的安装与配置、初始SQL语句、MySQL创建用户和授权
    前端收尾
  • 原文地址:https://www.cnblogs.com/gyy-15768200938/p/10976913.html
Copyright © 2011-2022 走看看