热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python学习笔记获取好友信息

参考链接:https:zhuanlan.zhihu.comp36418880一、代码实现:#-*-coding:utf-8-*-#微信撤回消息#p

参考链接:https://zhuanlan.zhihu.com/p/36418880
一、代码实现:

# -*- coding:utf-8 -*-
# 微信撤回消息
# python2
# 参考链接:https://zhuanlan.zhihu.com/p/36418880import itchat
itchat.login()# 爬取自己好友相关信息,返回一个json文件
friends = itchat.get_friends(update=True)[0:]
#print friends# 1、获得微信好友性别比例
male = female = other = 0
for i in friends[1:]: # friengs[0]是自己的信息sex = i['Sex']if sex == 1:male += 1elif sex == 2:female += 1else:other += 1total = len(friends[1:])
print "男性好友: %.2f%%" % (float(male)/total*100) + '\n' + "女性好友: %.2f%%" % (float(female)/total*100) + '\n' +\"不明性别好友: %.2f%%" % (float(other)/total*100)# 2、获得微信好友城市分布
# 首先定义一个函数爬去各个变量def get_var(var):variable = []for i in friends:value = i[var]if type(value) != int:value = value.encode('utf-8')variable.append(value)return variable
Nickname = get_var('NickName')
#print Nickname
Sex = get_var('Sex')
Province = get_var('Province')
City = get_var('City')
RemarkName = get_var('RemarkName') # 备注名
Signature = get_var('Signature')from pandas import DataFrame
data = {"NickName":Nickname,"Sex":Sex,"Province":Province,"City":City,"Signature":Signature,"RemarkName":RemarkName}
frame = DataFrame(data)
frame.to_csv('data.csv')# 3、微信好友签名自定义云图
# 首先将签名表示出来,并去除各种表情什么的,再用正则表达式去除《>=等符号
import re
siglist = []
for i in friends:signature &#61; i[&#39;Signature&#39;].encode(&#39;utf-8&#39;).strip().replace(&#39;span&#39;,&#39;&#39;).replace(&#39;class&#39;,&#39;&#39;).replace(&#39;emoji&#39;,"")print signature#siglist.append(signature)rep &#61; re.compile("1f\d&#43;\w*|[<>/&#61;]") # 正则表达式&#xff1f;&#xff1f;&#xff1f;没懂。。。signature &#61; rep.sub(&#39;&#39;,signature)siglist.append(signature)print siglist
text &#61; "".join(siglist)
print text.replace(" ","").replace("\n","")# 结巴分词包,对字符串进行分词
# 它是Python最好的中文分词组件&#xff1b;
import jieba
wordlist &#61; jieba.cut(text,cut_all&#61;True) # 全模式;False为精确模式&#xff1b;无为默认模式&#xff1b;cut_search_for()为搜索引擎模式
word_space_split &#61; " ".join(wordlist)
print word_space_split# 画图
import matplotlib.pyplot as plt
from wordcloud import WordCloud,ImageColorGenerator
import numpy as np
import PIL.Image as Imagecoloring &#61; np.array(Image.open(r"C:\Users\ZHYU\PycharmProjects\wechat\1.png"))
my_wordcloud &#61; WordCloud(background_color&#61;&#39;white&#39;,max_words&#61;2000,mask&#61;coloring,max_font_size&#61;60,random_state&#61;42,\scale&#61;2,font_path&#61;r"C:\Users\ZHYU\Downloads\NotoSans-hinted\NotoSans-Black.ttf").generate(word_space_split)
image_colors &#61; ImageColorGenerator(coloring)
plt.imshow(my_wordcloud.recolor(color_func&#61;image_colors))
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()itchat.logout()

运行结果&#xff1a;
这里写图片描述

二、遇到的问题
1、编码问题&#xff1a;由于用search_friends()得到的是所有好友信息的json数据&#xff0c;所以得到的昵称、签名等都是u’\u789’这种unicode形式&#xff0c;需要将其转为汉字表示时&#xff0c;用encode(“utf-8”)即可。

2、安装PIL遇到的问题&#xff0c;直接用pip安装后&#xff0c;运行报错&#xff1a;

ImportError: The _imagingft C module is not installed

经过百度找到了解决方案&#xff0c;在此链接上下载与你的系统对应的pillow版本
https://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow即可成功运行&#xff01;

3、最后的云图出现了乱码……此问题还未解决……下午再看


推荐阅读
author-avatar
杨艳奎_718
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有