第一个微信小项目
第一个好友分析:
我们需要用到wxpy这个库,这个库用到时会弹出一个二维码,这个二维码是通过扫码的方式登录微信,以获取信息
1 #导入模块 2 from wxpy import * 3 4 #初始化机器人,选择缓存模式(扫码)登录 5 bot = Bot(cache_path=True) 6 7 #获取我的所有微信好友信息 8 friend_all = bot.friends()
接下来我们调用这个库的函数做到将好友的信息转化成列表
1 lis=[] 2 for a_friend in friend_all: 3 NickName = a_friend.raw.get('NickName',None) 4 Sex ={1:"男",2:"女",0:"其它"}.get(a_friend.raw.get('Sex',None),None) 5 City = a_friend.raw.get('City',None) 6 Province = a_friend.raw.get('Province',None) 7 Signature = a_friend.raw.get('Signature',None) 8 list_0=[NickName,Sex,City,Province,Signature] 9 lis.append(list_0)
将列表转换为excl让我们方便看,我用到了pandas的一个小功能:
1 def toex(lis): 2 text=pd.DataFrame(lis,columns=['微信名','性别','城市','省份','个性签名']) 3 text.to_excel('wx.xlsx',encoding='\U0001f31a') 4 print(1)
结果如下:
词云如下:
1 import pandas as pd 2 from pyecharts import WordCloud 3 df=pd.read_excel('wx.xlsx') 4 city_list = df['城市'].fillna('city').tolist() 5 count_city = pd.value_counts(city_list) 6 name = count_city.index.tolist() 7 value = count_city.tolist() 8 wordcloud=WordCloud(width=1300, height=620) 9 wordcloud.add("", name, value, word_size_range=[20, 100]) 10 wordcloud.show_config() 11 wordcloud.render(r'wxcity.html') 12 print(1)
图如下:
地图如下:
1 import pandas as pd 2 from pyecharts import Map 3 df=pd.read_excel('wx.xlsx') 4 pr_list = df['省份'].fillna('pr').tolist() 5 count_pr = pd.value_counts(pr_list) 6 attr =count_pr.index.tolist() 7 value = count_pr.tolist() 8 maap=Map("各省微信好友分布", width=1200, height=600) 9 maap.add("", attr, value, maptype='china', is_visualmap=True,visual_text_color='#000', is_label_show = True) 10 maap.show_config() 11 maap.render(r'wxpr.html') 12 print(1)
结果如下:
微信机器人:
1 # 导入模块 2 from wxpy import * 3 import requests 4 import json 5 # 初始化机器人,扫码登陆 6 bot = Bot()
首先登陆并初始化机器人,接下来创建图灵机器人,大家要在图灵机器人的官网上获得apikey,下面是连接图灵机器人:
1 robot = Bot() 2 def talk_robot(info='你好啊'): #定义一个默认参数 3 api_url = 'http://www.tuling123.com/openapi/api' # 图灵接口url 4 apikey = 'xxxxxxxxxx' # 注册图灵生成key 5 data = {'key': apikey, 'info': info} 6 r = requests.post(api_url, data=data).text
之后我们有做了GUI编程的机器人界面,代码如下:
1 # -*- coding: utf-8 -*- 2 """ 3 Created on Sun Jun 14 11:51:24 2020 4 5 @author: 49594 6 """ 7 8 import wx 9 import urllib.request 10 import requests 11 import sys 12 class login(wx.Frame): 13 def __init__(self, parent, id, title): 14 wx.Frame.__init__(self, parent, id, title,size=(800,600),style=wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX)) 15 self.panel = wx.Panel(self, -1,size=(800,600)) 16 self.panel.SetBackgroundColour('white') 17 18 box=wx.BoxSizer(wx.VERTICAL) 19 boxsizer1 = wx.BoxSizer(wx.HORIZONTAL) 20 boxsizer2 = wx.BoxSizer(wx.HORIZONTAL) 21 self.chat=wx.StaticText(self.panel,-1,u'聊天框:') 22 self.chat_content=wx.TextCtrl(self.panel,-1,size=(735,500),style= wx.TE_READONLY|wx.TE_MULTILINE) 23 self.yousay=wx.StaticText(self.panel,-1,u'回 复:') 24 self.yousay_content=wx.TextCtrl(self.panel,-1,size=(650,45),style=wx.TE_PROCESS_ENTER) 25 self.yousay_submit=wx.Button(self.panel,-1,u'发送') 26 self.Bind(wx.EVT_BUTTON,self.submit,self.yousay_submit) 27 self.Bind(wx.EVT_TEXT_ENTER, self.submit, self.yousay_content) 28 29 30 boxsizer1.Add(self.chat,proportion=0,flag=wx.TOP,border=250) 31 boxsizer1.Add(self.chat_content,proportion=0,flag=wx.EXPAND,border=0) 32 33 boxsizer2.Add(self.yousay,proportion=0,flag=wx.TOP,border=10) 34 boxsizer2.Add(self.yousay_content,proportion=0,flag=wx.EXPAND,border=0) 35 boxsizer2.Add(self.yousay_submit,proportion=0,flag=wx.EXPAND,border=0) 36 37 box.Add(boxsizer1,proportion=0,flag=wx.TOP|wx.LEFT,border=5) 38 box.Add(boxsizer2,proportion=0,flag=wx.TOP|wx.LEFT,border=5) 39 self.chat_content.AppendText(u"提示:你正在和图灵机器人聊天\n") 40 self.SetSizer(box) 41 self.Center() 42 self.Show(True) 43 44 def submit(self,event): 45 yousay_content=self.yousay_content.GetValue() 46 if yousay_content == "bye": 47 sys.exit() 48 url="http://www.tuling123.com/openapi/api" 49 key="1f2e5957940c4c8381211ce4556f799d" 50 repson=requests.post(url+"?key="+key+"&info="+yousay_content).json() 51 res=repson.get("text") 52 53 54 55 56 self.chat_content.AppendText(u"你说:%s" % yousay_content) 57 self.chat_content.AppendText('\n') 58 self.chat_content.AppendText(u'机器人:%s' % res) 59 self.chat_content.AppendText('\n') 60 self.yousay_content.Remove(0,self.yousay_content.GetLastPosition()) 61 62 63 app = wx.App() 64 login(None, -1, u'聊天机器人') 65 app.MainLoop()
结果如下:
努力地向月光下的影子——骇客靠拢!!!
黎明之花,待时绽放