微信机器人
用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()