"IT有得聊」是機械工業出版社旗下IT專業資訊和服務平臺,致力於幫助讀者在廣義的IT領域裡,掌握更專業、實用的知識與技能,快速提升職場競爭力。 點擊藍色微信名可快速關注我們!
本文內容將通過一個具體實例講解將朋友圈數據導出為JSON文件的方法,並介紹使用Python統計分析JSON數據的過程。
註:本文中涉及的實例文件為《Python數據分析從入門到精通》隨書資源文件
使用開源工具WeChatMomentExport導出微信朋友圈數據,WeChatMomentExport的源碼地址和使用教程請參考https://github.com/Chion82/WeChatMomentExport
如圖1所示。
圖1.使用WeChatMomentExport導出數據
使用WeChatMomentExport可導出如下所示的分類信息,每一個分類信息都保存到對應的JSON文件中。
使用WeChatMomentExport導出數據後,接著使用Python統計分析朋友圈的各類信息。實例文件wechat_moment_stat.py的功能是分別統計處理7個JSON文件中的數據,然後輸出顯示統計結果。文件wechat_moment_stat.py的具體實現流程如下所示。
通過函數get_user()獲取每個朋友圈用戶的詳細信息,具體實現代碼如下所示。
def get_user(user_name):
foruser_info in result:
ifuser_info['user'] == user_name:
returnuser_info
user_info= {
'user': user_name,
'moments': [],
'post_comments': [],
'replied_comments': [],
'received_comments': [],
'post_likes': 0,
'received_likes': 0,
'spam_counts': 0,
}
result.append(user_info)
returnuser_info
編寫函數is_spam()提取朋友圈留言中的信息,具體實現代碼如下所示。
def is_spam(moment_text):
if ('投' in moment_text and '謝' in moment_text):
returnTrue
if ('投票' in moment_text):
returnTrue
if ('問卷' in moment_text):
returnTrue
if ('填' in moment_text and '謝' in moment_text):
returnTrue
returnFalse
編寫函數handle_moment()處理留言信息,具體實現代碼如下所示。
defhandle_moment(moment):
user_info= get_user(moment['author'])
user_info['moments'].append(moment)
user_info['received_likes']= user_info['received_likes'] + len(moment
['likes'])
user_info['received_comments'].extend(moment['comments'])
if(is_spam(moment['content'])):
user_info['spam_counts']= user_info['spam_counts'] + 1
forcomment_info in moment['comments']:
comment_user= get_user(comment_info['author'])
comment_user['post_comments'].append(comment_info)
if(comment_info['to_user'] != ''):
replied_user= get_user(comment_info['to_user'])
replied_user['replied_comments'].append(comment_info)
forlike_info in moment['likes']:
like_user= get_user(like_info)
like_user['post_likes']= like_user['post_likes'] + 1
排序處理7類信息,具體實現代碼如下所示。
for moment_info in origin_data:
handle_moment(moment_info)
f = open('user_output.json', 'w')
f.write(json.dumps(result))
f.close()
post_moment_rank = sorted(result, key=lambdauser_info: len(user_info
['moments']), reverse=True)
post_like_rank = sorted(result, key=lambda user_info:user_info['post_
likes'], reverse= True)
received_like_rank = sorted(result, key=lambdauser_info: user_info
['received_likes'], reverse=True)
post_comment_rank = sorted(result, key=lambdauser_info: len(user_info
['post_comments']), reverse=True)
received_comment_rank = sorted(result, key=lambdauser_info: len(user_info
['received_ comments']), reverse=True)
no_reply_rank = sorted(result, key=lambdauser_info: ((float(len(user_
info['replied_comments']))/len(user_info['post_comments'])) if len(user_info
['post_comments'])>0 else 999))
spam_rank = sorted(result, key=lambda user_info:user_info['spam_counts'],
reverse=True)
f = open('post_moment_rank.json', 'w')
f.write(json.dumps(post_moment_rank))
f.close()
列印輸出發送朋友圈信息最多的前5位數據,具體實現代碼如下所示。
print('前5位點讚數量最多:')
temp_list = []
for i in range(5):
temp_list.append(post_like_rank[i]['user']+ '(%d 贊)' % post_like_
rank[i]['post_likes'])
print(', '.join(temp_list))
f = open('received_like_rank.json', 'w')
f.write(json.dumps(received_like_rank))
f.close()
列印輸出前5位獲得最多贊的用戶信息,具體實現代碼如下所示。
print('前5位獲得最多贊:')
temp_list = []
for i in range(5):
temp_list.append(received_like_rank[i]['user']+ '(%d 贊)' % received_
like_rank[i] ['received_likes'])
print(', '.join(temp_list))
f = open('post_comment_rank.json', 'w')
f.write(json.dumps(post_comment_rank))
f.close()
列印輸出前5位評論數量最多的用戶信息,具體實現代碼如下所示。
print('前5位評論數量最多:')
temp_list = []
for i in range(5):
temp_list.append(post_comment_rank[i]['user']+ '(%d 評論)' % len(post_
comment_rank[i]['post_comments']))
print(', '.join(temp_list))
f = open('received_comment_rank.json', 'w')
f.write(json.dumps(received_comment_rank))
f.close()
列印輸出前5位朋友圈評論最多的用戶信息,具體實現代碼如下所示。
print('前5位朋友圈評論最多:')
temp_list = []
for i in range(5):
temp_list.append(received_comment_rank[i]['user']+ '(%d 評論)' % len
(post_comment_rank[i]['received_comments']))
print(', '.join(temp_list))
f = open('no_reply_rank.json', 'w')
f.write(json.dumps(no_reply_rank))
f.close()
f = open('spam_rank.json', 'w')
f.write(json.dumps(spam_rank))
f.close()
列印輸出收到評論回複數/寫評論數前5名且發出評論數>=15用戶的信息,具體實現代碼如下所示。
print('================================')
print('前5名(收到評論回複數/寫評論數且 發出評論數>=15):')
temp_list = []
for user_info in no_reply_rank:
iflen(user_info['post_comments']) < 15:
continue
if (len(temp_list) > 5):
break
temp_list.append(user_info['user']+ ('(收到評論回復%d, 寫評論%d)' % (len
(user_info['replied_comments']), len(user_info['post_comments']))))
print(', '.join(temp_list))
執行後列印輸出朋友圈的統計結果如下所示。
前5位發最多朋友圈:
Joe(69 條), xxx (62 條), 嬴子夜。(58 條), xxx(46 條), psh(40 條)
前5位點讚數量最多:
Saruman(33 贊), xxx(28贊),ChiaChia.Ý(27贊),❄️Max❄(27 贊), 郭含陽(23贊)
前5位獲得最多贊:
陳思君(38 贊), 404(32 贊), (29 贊), Justin Tan(27 贊), 楊宗煒 (26 贊)
前5位評論數量最多:
楊宗煒 (110 評論), 404(95 評論), Saruman(94 評論), MATTHEW °Д°(77 評論), Joe(57 評論)
前5位朋友圈評論最多:
楊宗煒 (209 評論), 404(130 評論), Joe(37 評論), MATTHEW °Д°(68 評論), ❄️Max❄(69 評論)
================================
收到評論回複數/寫評論數且 發出評論數>=15:
Joe(收到評論回復13, 寫評論57), jjy(收到評論回復4, 寫評論16), Saruman(收到評論回復30, 寫評論94), 璽璽璽(收到評論回復7, 寫評論21), ChiaChia.Ý(收到評論回復6, 寫評論15), 郭含陽(收到評論回復14, 寫評論33)
識別二維碼可觀看本案例演示視頻
毫無疑問,Python 是當下最火的程式語言之一。對於許多未曾涉足計算機編程領域的「小白」來說,深入地掌握 Python 看似是一件十分困難的事。其實,只要掌握科學的學習方法並制定合理的學習計劃再加上堅持不懈的努力,你就可以通過學習Python,走進編程的殿堂。
那麼,作為「小白」,在剛剛接觸Python數據分析的時候,需要注意什麼,需要從哪些知識學起呢?為了幫助大家更好地解決這些問題我們將面向廣大Python學習者開放一個全新的視頻直播課程——Python開發從入門到精通系列課程。
本課程分為三講:包含基礎知識、Web開發、數據分析三大板塊。
本周四(7月16日)
20:00—21:00
本系列課程的第二講
《小白如何學習Python數據分析》
將從就業前景、應用領域、數據存儲方式、數據可視化、實例演示等幾方面入手,幫助讀者快速找到Python數據分析學習方法。
從你開始學習編程的那一刻起,就註定了以後所要走的路:從編程學習者開始,依次經歷實習生、程式設計師、軟體工程師、架構師、CTO等職位的磨礪;當你站在職位頂峰的位置驀然回首,會發現自己的成功並不是偶然,在程式設計師的成長之路上會有不斷修改代碼、尋找並解決Bug、不停測試程序和修改項目的經歷;不可否認的是,只要你在自己的程序開發生涯中穩紮穩打,並且善於總結和學習,最終將會得到應用的收穫。
本節課程聚焦當下熱門程式語言Python,從就業前景、應用領域、數據存儲方式、數據可視化、實例演示幾方面入手,幫助讀者找到Python數據分析學習路徑。
開放時間:2020年7月16日20:00
主講老師:管西京
課程類型:視頻直播
觀看平臺: 九州雲播平臺
京東直播平臺
機械工業出版社天貓旗艦店
機械工業出版社官方騰訊直播
機械工業出版社百度百家號直播間
管西京,山東大學計算機碩士,精通Python、Java、C#、C語言、C++等主流程式語言,擅長底層技術和應用程式的結合運用,具有豐富的Python數據分析、運維自動化和雲計算開發經驗。現就職於浪潮信息研發中心,負責浪潮雲的開發和維護工作。
相關著作:
★ 小白如何學習Python數據分析
1) Python數據分析相關人才的就業前景
2) Python數據分析技能應用領域
3) 數據存儲方式
4) 數據可視化
5) 實例演示
方法一:關注「IT有得聊」公眾號,我們會於直播前放出各平臺直播間二維碼。
方法二(牆裂推薦):
微信掃描下方二維碼,添加小編微信,發送「Python」給小編(注意,一定要發送「Python」),加入直播交流群。
入群有福利:
☆直播期間,入群讀者的提問,專家會在直播間內優先解答。
☆直播結束後,本次分享的課件會分享到群中。
☆可獲取更多優質課程分享
點擊圖片可進入直播回看連結文章
識別文章內二維碼可觀看回看