发布时间:2026-07-21阅读(0)
一共就两个文件,完整下载地址,在文章末尾贴出,接下来我们就来聊聊关于微信怎么生成随机id?以下内容大家不妨参考一二希望能帮到您!

微信怎么生成随机id
一共就两个文件,完整下载地址,在文章末尾贴出。
统计微信好友信息,包括性别比例、地区分布、签名统计、词云图分析好友特点
需要
Windows python3.5、3.6
pip install wxpy
pip install jieba
pip install pandas
pip install numpy
pip install scipy
pip install wordcloud
部分源码:
import re
10 from wxpy import *
11 import jieba
12 import numpy
13 import pandas as pd
14 import matplotlib.pyplot as plt
15 from scipy.misc import imread
16 from wordcloud import WordCloud, ImageColorGenerator
17 def write_txt_file(path, txt):
18
19 写入txt文本
20
21 with open(path, a, encoding=gb18030, newline=) as f:
22 f.write(txt)
23 def read_txt_file(path):
24
25 读取txt文本
26
27 with open(path, r, encoding=gb18030, newline=) as f:
28 return f.read()
29 def login():
30 # 初始化机器人,扫码登陆
31 bot = Bot()
32 # 获取所有好友
33 my_friends = bot.friends()
34 print(type(my_friends))
35 return my_friends
36 def show_sex_ratio(friends):
37 # 使用一个字典统计好友男性和女性的数量
38 sex_dict = {male: 0, female: 0}
39 for friend in friends:
40 # 统计性别
41 if friend.sex == 1:
42 sex_dict[male] = 1
43 elif friend.sex == 2:
44 sex_dict[female] = 1
45 print(sex_dict)
46 def show_area_distribution(friends):
47 # 使用一个字典统计各省好友数量
48 province_dict = {北京: 0, 上海: 0, 天津: 0, 重庆: 0,
49 河北: 0, 山西: 0, 吉林: 0, 辽宁: 0, 黑龙江: 0,
50 陕西: 0, 甘肃: 0, 青海: 0, 山东: 0, 福建: 0,
51 浙江: 0, 台湾: 0, 河南: 0, 湖北: 0, 湖南: 0,
52 江西: 0, 江苏: 0, 安徽: 0, 广东: 0, 海南: 0,
53 四川: 0, 贵州: 0, 云南: 0,
54 内蒙古: 0, 新疆: 0, 宁夏: 0, 广西: 0, 西藏: 0,
55 香港: 0, 澳门: 0}
56 # 统计省份
57 for friend in friends:
58 if friend.province in province_dict.keys():
59 province_dict[friend.province] = 1
60 # 为了方便数据的呈现,生成JSON Array格式数据
61 data = []
62 for key, value in province_dict.items():
63 data.append({name: key, value: value})
64 print(data)
65 def show_signature(friends):
66 # 统计签名
67 for friend in friends:
68 # 对数据进行清洗,将标点符号等对词频统计造成影响的因素剔除
69 pattern = re.compile(r[一-龥] )
70 filterdata = re.findall(pattern, friend.signature)
71 write_txt_file(signatures.txt, .join(filterdata))
72 # 读取文件
73 content = read_txt_file(signatures.txt)
74 segment = jieba.lcut(content)
75 words_df = pd.DataFrame({segment:segment})
76 # 读取stopwords
77 stopwords = pd.read_csv("stopwords.txt",index_col=False,quoting=3,sep=" ",names=[stopword],encoding=utf-8)
78 words_df = words_df[~words_df.segment.isin(stopwords.stopword)]
79 print(words_df)
80 words_stat = words_df.groupby(by=[segment])[segment].agg({"计数":numpy.size})
81 words_stat = words_stat.reset_index().sort_values(by=["计数"],ascending=False)
82 # 设置词云属性
83 color_mask = imread(background.jfif)
84 wordcloud = WordCloud(font_path="simhei.ttf", # 设置字体可以显示中文
85 background_color="white", # 背景颜色
86 max_words=100, # 词云显示的最大词数
87 mask=color_mask, # 设置背景图片
88 max_font_size=100, # 字体最大值
89 random_state=42,
90 width=1000, height=860, margin=2,# 设置图片默认的大小,但是如果使用背景图片的话, # 那么保存的图片大小将会按照其大小保存,margin为词语边缘距离
91 )
92 # 生成词云, 可以用generate输入全部文本,也可以我们计算好词频后使用generate_from_frequencies函数
93 word_frequence = {x[0]:x[1]for x in words_stat.head(100).values}
94 print(word_frequence)
95 word_frequence_dict = {}
96 for key in word_frequence:
97 word_frequence_dict[key] = word_frequence[key]
98 wordcloud.generate_from_frequencies(word_frequence_dict)
99 # 从背景图片生成颜色值
100 image_colors = ImageColorGenerator(color_mask)
101 # 重新上色
102 wordcloud.recolor(color_func=image_colors)
103 # 保存图片
104 wordcloud.to_file(output.png)
105 plt.imshow(wordcloud)
106 plt.axis("off")
107 plt.show()
108 def main():
109 friends = login()
110 show_sex_ratio(friends)
111 show_area_distribution(friends)
112 show_signature(friends)
113 if __name__ == __main__:
114 main()
源码地址:https://github.com/xungeer29/Information-Statistics-of-WeChat-Friends-with-Python.git
Copyright © 2024 有趣生活 All Rights Reserve吉ICP备19000289号-5 TXT地图HTML地图XML地图