一、微信好友分析
1、统计好友,城市分布,个性签名并保存为excell
#导入模块 from wxpy import * #初始化机器人,选择缓存模式(扫码)登录 bot = Bot(cache_path=True) #获取我的所有微信好友信息 friend_all = bot.friends() len(friend_all) lis=[] for a_friend in friend_all: NickName = a_friend.raw.get(‘NickName‘,None) #Sex = a_friend.raw.get(‘Sex‘,None) Sex ={1:"男",2:"女",0:"其它"}.get(a_friend.raw.get(‘Sex‘,None),None) City = a_friend.raw.get(‘City‘,None) Province = a_friend.raw.get(‘Province‘,None) Signature = a_friend.raw.get(‘Signature‘,None) HeadImgUrl = a_friend.raw.get(‘HeadImgUrl‘,None) HeadImgFlag = a_friend.raw.get(‘HeadImgFlag‘,None) list_0=[NickName,Sex,City,Province,Signature,HeadImgUrl,HeadImgFlag] lis.append(list_0) def lis2e07(filename,lis): import openpyxl wb = openpyxl.Workbook() sheet = wb.active sheet.title = ‘list2excel07‘ 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(‘wechat‘,lis)
效果如下:
2、将城市信息做成词云并保存为html
代码如下:
import pandas as pd from wordcloud import WordCloud import matplotlib.pyplot as plt from pyecharts import WordCloud from pandas import read_excel df=pd.read_excel(r‘C:\Users\admin\.spyder-py3\wechat.xlsx‘) count=df.City.value_counts() #对 dataframe 进行全频率统计,排除了 nan city_list=df[‘City‘].fillna(‘NAN‘).tolist() count_city=pd.value_counts(city_list)#对 list 进行全频率统计 name=count_city.index.tolist() value=count_city.tolist() wordcloud=WordCloud(width=1300,height=620) wordcloud.add("", name,value,word_size_range=[20,100]) wordcloud.show_config() wordcloud.render(r‘C:\Users\admin\.spyder-py3\wcl.html‘)
我的电脑出了问题qaq
3、将好友显示在地图上并保存为html
代码如下:
from pandas import read_excel import pandas as pd from wordcloud import WordCloud import matplotlib.pyplot as plt df=pd.read_excel(r‘C:\Users\admin\.spyder-py3\wechat.xlsx) #将这些个好友在全国地图上做分布 province_list = df[‘Province‘].fillna(‘NAN‘).tolist()#将 dataframe 的列转化为 list,其中的 nan 用“NAN”替换 count_province = pd.value_counts(province_list)#对 list 进行全频率统计 from pyecharts import Map value =count_province.tolist() attr =count_province.index.tolist() map=Map("各省微信好友分布", width=1200, height=600) map.add("", attr, value, maptype=‘china‘, is_visualmap=True, visual_text_color=‘#000‘, is_label_show = True) #显示地图上的省份 map.show_config() map.render(r‘C:\Users\admin\.spyder-py3\map1.html‘)
无效果图
二、微信机器人
代码如下
import itchat,code,unicodedata
import requests,re
from urllib.parse import quote,unquote
def get_reply(data):
ini = "{‘sessionId‘:‘09e2aca4d0a541f88eecc77c03a8b393‘,‘robotId‘:‘webbot‘,‘userId‘:‘462d49d3742745bb98f7538c42f9f874‘,‘body‘:{‘content‘:‘" + data + "‘},‘type‘:‘txt‘}&ts=1529917589648"
url = "http://i.xiaoi.com/robot/webrobot?&callback=__webrobot_processMsg&data=" + quote(ini)
cookie = {"cnonce": "808116", "sig": "0c3021aa5552fe597bb55448b40ad2a90d2dead5",
"XISESSIONID": "hlbnd1oiwar01dfje825gavcn", "nonce": "273765", "hibext_instdsigdip2": "1"}
r = requests.get(url, cookies=cookie)
print(r.text)
pattern = re.compile(r‘\"fontColor\":0,\"content\":\"(.*?)\"‘)
result = pattern.findall(r.text)
print(result[1])
return result[1]
@itchat.msg_register(itchat.content.TEXT)
def print_content(msg):
print(msg[‘Text‘])
print(msg[‘FromUserName‘])
datas=get_reply(msg[‘Text‘])[:-4]
print(datas)
itchat.send(datas, toUserName=msg[‘莫雨林‘])a
itchat.auto_login()
itchat.run()