Python利用itchat對微信好友分析(window,PyCharm)

一、前言

今天是假期的第七天,厭倦那些無聊的生活。於是對着實驗樓的教程,就開幹了。

那做好的感受是什麼?

其實,談不上什麼收穫,都是抄別人的腳本,再調試一波。

如果問學到了什麼,也沒學到什麼。

二、實驗效果

三、實驗過程

1.搭建虛擬環境,並且安裝相應的包

--打開cmd:

  • mkvirtualenv wechats
  • pip install itchat pillow
  • pip install pyecharts pyecharts_snapshot echarts-countries-pypkg echarts-china-provinces-pypkg echarts-china-cities-pypkg

 2.在pycharm上新建“純python”項目,添加相應的虛擬環境

 3.新建python文件,輸入一下代碼:

# -*- coding:utf-8 -*-

import itchat
from collections import Counter
from pyecharts import Bar
import re
from pyecharts import Map

# 實現獲取朋友指定信息的方法
def get_key_info(friends_info, key):
    return list(map(lambda friend_info: friend_info.get(key), friends_info))


# 獲取朋友的相關信息,生成一個 {key:[value1,value2,...],} 類型的字典,最後返回該字典
def get_friends_info(friends):
    friends_info = dict(
    username=get_key_info(friends, 'UserName'), # 用戶名
    sex=get_key_info(friends, 'Sex'), # 性別
    province=get_key_info(friends, 'Province'), # 省份
    city=get_key_info(friends, 'City') # 城市
)
    return friends_info

if __name__ == '__main__':
    itchat.auto_login(hotReload=True)
    friends = itchat.get_friends(update=True)

from pyecharts import Page, Pie

# 處理數據
def count_nums(new_list):
    new_dict = {}
    for i in new_list:
        if bool(re.search('[a-z]|[A-Z]', i)): # 如果帶英文字母(要麼是國外,要麼是亂寫的)就跳出本次循環
            continue
        elif not new_dict.__contains__(i):
            new_dict[i] = 1
        else:
            new_dict[i] += 1
    new_dict.pop('') # 去掉空的鍵
    return new_dict


def analysis(friends):
    friends_info = get_friends_info(friends)

# 男女性別比例
    sex_list = friends_info['sex']

    sex_dict = dict(Counter(sex_list))
    attr = ["未知", "男性", "女性"]
    value = [sex_dict[0], sex_dict[1], sex_dict[2]]
    page = Page()
    chart1 = Pie("微信好友性別比例圖",title_pos='center') #標題放在中間
    chart1.add("", attr, value, is_label_show=True, legend_orient="vertical", legend_pos="left") #顯示標籤,圖例組件垂直分佈,圖例位於左側
    page.add(chart1) #把chart1添加到頁面中

 #中國省級分析
    province_list = friends_info['province']
    province_dict = count_nums(province_list)
    attr, value = list(province_dict.keys()), list(province_dict.values())
    # 中國省級分析畫圖
    chart2 = Map('好友省級分佈(中國地圖)', width=1200, height=600) #設置圖像的長和高的像素值
    chart2.add('', attr, value, maptype='china', is_label_show=True, is_visualmap=True, visual_text_color='#000') #地圖類型爲中國,顯示標籤數據,使用視覺映射組件,文本顏色爲黑色
    page.add(chart2) #把chart2添加到頁面中

# 中國城市分析(取前10個人數最多的城市)
    city_list = friends_info['city']
    city_dict = count_nums(city_list)
    top_ten_city = dict(sorted(city_dict.items(), key=lambda x: x[1], reverse=True)[0:10])
    attr, value = list(top_ten_city.keys()), list(top_ten_city.values())
# 中國城市分析畫圖
    chart3 = Bar('好友城市分佈Top10柱狀圖', width=900, height=500) #設置圖形的長和寬
    chart3.add('', attr, value, is_stack=False,is_label_show=True,bar_category_gap='20%') #把數據堆疊設置爲False,顯示標籤,類目軸的柱狀距離爲20%
    page.add(chart3) #把chart3添加到頁面中

    page.render('analysisResult.html') #生成整個頁面,命名爲analysisResult.html




if __name__ == '__main__':
    itchat.auto_login(hotReload=True)
    friends = itchat.get_friends(update=True)

    analysis(friends)

4.運行,同目錄下自動生成HTML文件,再運行HTML頁面即可

 四、總結

  • 學習時不能急於求成,上面就是一種急於求成的做法。照着上面的教程,對,你做出來了,你能跟別人解釋清楚嗎?那些代碼是什麼意思?能用在什麼場景?等等,這都需要你思考。
  • 腳本是從上往下執行,前面的函數不能調用後面的函數。如下如:

  • 雖然實驗樓的教程是把分析方法分成很多部分了,你得整理成一個函數
  • 注意縮進
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章