網(wǎng)站性能檢測評分
注:本網(wǎng)站頁面html檢測工具掃描網(wǎng)站中存在的基本問題,僅供參考。
python分析
用python分析你的朋友圈,很好玩~ 朋友圈視頻課程
設(shè)計(jì)喵的內(nèi)心OS:我一臉懵逼點(diǎn)進(jìn)來,一臉懵逼走出去,你說了什么?
事情是這么來的,我看到有朋友在做朋友圈的電子書,也想自己研究一下,發(fā)現(xiàn)原來有個itchart這個微信第三方API可以讀取微信數(shù)據(jù),抱著好奇的想法嘗試了以下,果然非常好玩。
get
Friends
List.py這個類用來爬取好友信息并保存到指定文件
import
itchat
import
os
import
time
basepath
= os.path.dirname(os.path.realpath(__file__))
download_path = basepath+
'/downloads'
+
'/'
#調(diào)用itchat接口登錄并拉取數(shù)據(jù)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這個類根據(jù)下載的好友數(shù)據(jù)分析好友信息
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')
#初始化計(jì)數(shù)器male=female= other =0#所有省份Provinces_list =
#friends[0]是自己的信息,所以要從friends[1]開始foriinfriends:
ifi.__len__>0:
i = i.replace('i = i.replace(']>'']>"')
friend =eval(i)
#統(tǒng)計(jì)性別sex = friend["Sex"]
ifsex ==1:
male +=1exitelifsex ==2:
female +=1else:
other+=1#統(tǒng)計(jì)地區(qū)Province = friend["Province"]
Provinces_list.append(Province)
#計(jì)算朋友總數(shù)total=len(friends)
#打印出自己的好友性別比例print("總好友數(shù): %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("==========人數(shù)排名前10地區(qū)如下============")
top =0fork,vinProvinces_dict:
iftop
print("%s : %d個,占比 : %.2f%%"%
(k,v,float(v)/total*100))
top+=1
輸出結(jié)果:
總好友數(shù):1324
男性好友:680個,占比 51.36%
女性好友:592個,占比 44.71%
不明性別好友:51個,占比 3.85%
=======人數(shù)排名前10地區(qū)如下========
未知: 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根據(jù)簽名生成詞云
詞云來自全部好友的簽名,把簽名收集在一起拼接字符串然后分詞再拼成一張圖片。重復(fù)越多的字越大。
# -*- 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開發(fā)的我經(jīng)過測試已經(jīng)被官方黑名單了,,不過用來自娛自樂很奈斯。
還能分析你朋友圈全部的內(nèi)容,圖片、文字哪些做微信電子書就是用這個原理做的爬蟲。
原文鏈接是我用js實(shí)現(xiàn)的微信跳轉(zhuǎn)支付寶demo有興趣的可以體驗(yàn)一下,明天文章發(fā)教程。
感覺要轉(zhuǎn)行做開發(fā)了的感覺~