第一个微信小项目
获取好友信息:
#导入模块 from wxpy import * #初始化机器人,选择缓存模式(扫码)登录 bot = Bot(cache_path=True) #获取我的所有微信好友信息 friend_all = bot.friends() print(friend_all[0].raw) print(len(friend_all)) #查看获取的好友总数 lis=[] for a_friend in friend_all: NickName = a_friend.raw.get('NickName',None)#昵称 Sex ={1:"男",2:"女",0:"其它"}.get(a_friend.raw.get('Sex',None),None)#性别 Signature = a_friend.raw.get('Signature', None)#个性签名 Province = a_friend.raw.get('Province',None)#省份 City = a_friend.raw.get('City',None)#城市 list_0=[NickName,Sex,Signature,Province,City] #提取好友的昵称、性别、所在城市、省份、备注名字 lis.append(list_0)
生成文件并输出城市排名:
#导入模块 from wxpy import * #初始化机器人,选择缓存模式(扫码)登录 bot = Bot(cache_path=True) #获取我的所有微信好友信息 friend_all = bot.friends() print(friend_all[0].raw) print(len(friend_all)) #查看获取的好友总数 lis=[] for a_friend in friend_all: NickName = a_friend.raw.get('NickName',None)#昵称 Sex ={1:"男",2:"女",0:"其它"}.get(a_friend.raw.get('Sex',None),None)#性别 Signature = a_friend.raw.get('Signature', None)#个性签名 Province = a_friend.raw.get('Province',None)#省份 City = a_friend.raw.get('City',None)#城市 list_0=[NickName,Sex,Signature,Province,City] #提取好友的昵称、性别、所在城市、省份、备注名字 lis.append(list_0) #提取好友信息放入列表,每个好友的这些信息的值做成了一个列表,将所有好友的列表做成一个大列表 lis 的元素 def lis2e07(filename,lis): import openpyxl wb=openpyxl.Workbook() sheet = wb.active sheet.title = '宇' file_name = filename +'.xlsx' for i in range(0,len(lis)): for j in range(0,len(lis[i])): sheet.cell(row=i+1, column=j+1, value=str(lis[i][j])) wb.save(file_name) print("写入数据成功!") lis2e07('宇',lis) Friends =bot.friends() data = Friends.stats_text(total=True, sex=True,top_provinces=30, top_cities=500) print(data)
生成词云图:
from wxpy import * import jieba from wordcloud import WordCloud import matplotlib.pyplot as plt #初始化机器人,选择缓存模式(扫码)登录 bot = Bot(cache_path=True) #获取我的所有微信好友信息 friend_all = bot.friends() print(friend_all[0].raw) print(len(friend_all)) lis=[] for a_friend in friend_all: NickName = a_friend.raw.get('NickName',None)#昵称 Sex ={1:"男",2:"女",0:"其它"}.get(a_friend.raw.get('Sex',None),None)#性别 Signature = a_friend.raw.get('Signature', None)#个性签名 Province = a_friend.raw.get('Province',None)#省份 City = a_friend.raw.get('City',None)#城市 list_0=[NickName,Sex,Signature,Province,City] #提取好友的昵称、性别、所在城市、省份、备注名字 lis.append(list_0) cityStr = "" for i in range(len(friend_all)): if lis[i][4] not in cityStr: cityStr += " " + lis[i][4] #jieba库精确模式分词 wordlist = jieba.lcut(cityStr) cityStr = ' '.join(wordlist) # 加载背景图片 #cloud_mask = np.array(Image.open(BackGroundFile)) #设置词云图属性 font = r'C:\Windows\Fonts\simfang.ttf' # 设置字体路径 wc = WordCloud( background_color = 'black', # 背景颜色 #mask = cloud_mask, # 背景图片 max_words = 100, # 设置最大显示的词云数 font_path = font, # 设置字体形式(在本机系统中) height = 300, # 图片高度 width = 600, # 图片宽度 max_font_size = 100, # 字体最大值 random_state = 100, # 配色方案的种类 ) # 生成词云图 myword = wc.generate(cityStr) #展示词云图 plt.imshow(myword) plt.axis('off') plt.show()
html地图:
#导入模块 from wxpy import * #初始化机器人,选择缓存模式(扫码)登录 bot = Bot(cache_path=True) #获取我的所有微信好友信息 friend_all = bot.friends() print(friend_all[0].raw) print(len(friend_all)) #查看获取的好友总数 lis=[] for a_friend in friend_all: NickName = a_friend.raw.get('NickName',None)#昵称 Sex ={1:"男",2:"女",0:"其它"}.get(a_friend.raw.get('Sex',None),None)#性别 Signature = a_friend.raw.get('Signature', None)#个性签名 Province = a_friend.raw.get('Province',None)#省份 City = a_friend.raw.get('City',None)#城市 list_0=[NickName,Sex,Signature,Province,City] #提取好友的昵称、性别、所在城市、省份、备注名字 lis.append(list_0) from pyecharts import Map provinceList, provinceNum = [], [] for i in range(len(friend_all)): if lis[i][3] not in provinceList: provinceList.append(lis[i][3]) provinceNum.append(0) for i in range(len(friend_all)): for j in range(len(provinceList)): if lis[i][3] == provinceList[j]: provinceNum[j] += 1 # 生成 Map map = Map("各省微信好友分布", width=1000, height=800) map.add("", provinceList, provinceNum, maptype="china", is_visualmap=True, visual_text_color='#000') map.show_config() map.render(r'宇.html') print(1)
创建图灵机器人:
from wxpy import Bot,Tuling,embed,ensure_one bot = Bot() my_friend = ensure_one(bot.search('顾嘉伟')) #想和机器人聊天的好友的备注 tuling = Tuling(api_key='ee8fb3854a6d40c1a0e42f57d7ac0342') @bot.register(my_friend) # 使用图灵机器人自动与指定好友聊天 def reply_my_friend(msg): tuling.do_reply(msg) embed()