中企动力 > 头条 > python分析

网站性能检测评分

注:本网站页面html检测工具扫描网站中存在的基本问题,仅供参考。

python分析

用python分析你的朋友圈,很好玩~ 朋友圈视频课程

img

思卉

关注

设计喵的内心OS:我一脸懵逼点进来,一脸懵逼走出去,你说了什么?

事情是这么来的,我看到有朋友在做朋友圈的电子书,也想自己研究一下,发现原来有个itchart这个微信第三方API可以读取微信数据,抱着好奇的想法尝试了以下,果然非常好玩。

get

Friends

List.py这个类用来爬取好友信息并保存到指定文件

import

itchat

import

os

import

time

basepath

= os.path.dirname(os.path.realpath(__file__))

download_path = basepath+

'/downloads'

+

'/'

#调用itchat接口登录并拉取数据itchat.login

friends = itchat.get_friends(update=True)[0:]

fmt='%Y%m%d%H%M%S'#定义时间显示格式Date=time.strftime(fmt,time.localtime(time.time))

download_file_name ='friendsList_'+friends[0]['NickName']

+'_'+ Date +'.txt'

f =open(download_path+download_file_name,'wb')

print(download_path+download_file_name)

foriinfriends[1:]:

friend = (str(i) +"\n").encode(encoding='gb18030')

# print(str(i))

f.write(friend)

f.close

analyse.py这个类根据下载的好友数据分析好友信息

importre

importos

importtime

source_file ="friendslist_阿西UED_20180105191247.txt"basepath = os.path.dirname(os.path.realpath(__file__))

download_file = basepath+'/downloads/'+ source_file

fs_str =''withopen(download_file,'rb')asf:

fs_str = f.read.decode('gb18030')

friends = fs_str.split('\n')

#初始化计数器male=female= other =0#所有省份Provinces_list =

#friends[0]是自己的信息,所以要从friends[1]开始foriinfriends:

ifi.__len__>0:

i = i.replace('i = i.replace(']>'']>"')

friend =eval(i)

#统计性别sex = friend["Sex"]

ifsex ==1:

male +=1exitelifsex ==2:

female +=1else:

other+=1#统计地区Province = friend["Province"]

Provinces_list.append(Province)

#计算朋友总数total=len(friends)

#打印出自己的好友性别比例print("总好友数: %d"% total +"\n"+

"男性好友: %d个,占比 %.2f%%"% (male,(float(male)

/total*100)) +"\n"+

"女性好友: %d个,占比 %.2f%%"% (female,(float(female) / total *100)) +"\n"+

"不明性别好友: %d个,占比 %.2f%%"% (other,

(float(other) / total *100)))

Provinces_set =set(Provinces_list)

Provinces_dict = {}

foriinProvinces_set:

Provinces_dict[i] = Provinces_list.count(i)

#对省份字典按value排序Provinces_dict =sorted(Provinces_dict.items,

key=lambdaasd:asd[1],reverse=True)

print("==========人数排名前10地区如下============")

top =0fork,vinProvinces_dict:

iftop

print("%s : %d个,占比 : %.2f%%"%

(k,v,float(v)/total*100))

top+=1

输出结果:

总好友数:1324

男性好友:680个,占比 51.36%

女性好友:592个,占比 44.71%

不明性别好友:51个,占比 3.85%

=======人数排名前10地区如下========

未知: 258个,占比 : 19.49%

北京 : 211个,占比 : 15.94%

上海 : 198个,占比 : 14.95%

广东 : 173个,占比 : 13.07%

河南 : 68个,占比 : 5.14%

浙江 : 63个,占比 : 4.76%

江苏 : 51个,占比 : 3.85%

四川 : 24个,占比 : 1.81%

山东 : 21个,占比 : 1.59%

河北 : 19个,占比 : 1.44%

wordCloud.py根据签名生成词云

词云来自全部好友的签名,把签名收集在一起拼接字符串然后分词再拼成一张图片。重复越多的字越大。

# -*- coding:UTF-8 -*-importre

importos

importtime

base_pic ="alice_coloring.jpg"source_file ="friendsList_阿西UED_20180105191247.txt"basepath = os.path.dirname(os.path.realpath(__file__))

download_file =basepath+'/downloads/'+ source_file

fs_str =''withopen(download_file,'rb')asf:

fs_str = f.read.decode('gb18030')

friends = fs_str.split('\n')

siglist =

foriinfriends:

ifi.__len__>0:

i = i.replace('i = i.replace(']>'']>"')

friend =eval(i)

# print(friend)# print(friend["Signature"])signature = friend["Signature"].strip

.replace("span""").replace("class""")

.replace("emoji""")

rep = repile("1f\d+\w*|[/=]")

signature = rep.sub("",signature)

siglist.append(signature)

text ="".join(siglist)

importjieba

wordlist = jieba.cut(text,cut_all=True)

word_space_split =" ".join(wordlist).replace("\n""")

print(word_space_split)

importmatplotlib.pyplotasplt

fromwordcloudimportWordCloud, ImageColorGenerator

importnumpyasnp

importPIL.ImageasImage

coloring = np.array(Image.open(base_pic))

my_wordcloud = WordCloud(background_color="white"

max_words=2000mask=coloring,max_font_size=60

random_state=42scale=2

font_path="fonts/STHeiti Light.ttc")

.generate(word_space_split)

image_colors = ImageColorGenerator(coloring)

plt.imshow(my_wordcloud.recolor(color_func=image_colors))

plt.imshow(my_wordcloud)

plt.axis("off")

plt.show词云的效果:

python还有其他比较好玩的东西,比如小程序辅助就是python开发的我经过测试已经被官方黑名单了,,不过用来自娱自乐很奈斯。

还能分析你朋友圈全部的内容,图片、文字哪些做微信电子书就是用这个原理做的爬虫。

原文链接是我用js实现的微信跳转支付宝demo有兴趣的可以体验一下,明天文章发教程。

感觉要转行做开发了的感觉~

img

在线咨询

建站在线咨询

img

微信咨询

扫一扫添加
动力姐姐微信

img
img

TOP