用python分析微信朋友圈

1.朋友圈好友数据提取与存储

这个功能可分为三步实现:

  • 模块一: 登录模块
import itchat
def get_data():
    itchat.auto_login()
    friends=itchat.get_friends(update=True)
    return friends
  • 模块二:数据提取模块

​​​​​​​        将好友的姓名、备注、性别、省份城市、个签等信息放入字典friend中,再将friend放入列表friends中。

def parse_data(data):
    friends=[]
    for item in data:
        friend={
            '姓名':item['NickName'],
            '备注':item['RemarkName'],
            '性别':item['Sex'],
            '省份':item['Province'],
            '城市':item['City'],
            '个性签名':item['Signature'].replace('\n',' ').replace(',',' '),
        }
        print(friend)
        friends.append(friend)
    return friends
  • 模块三:数据存储模块

​​​​​​​        在根目录下新建一个txt文档(记得存储为utf-8格式),将每个好友的姓名、备注等以分号为分隔写入文档(分隔符没有固定的要求,但最好不要用空格,因为有些好友名字里有空格,会导致提取数据错误。)

def save_to_txt():
    friends = parse_data(get_data())
    for item in friends:
        with open('friend.txt', mode='a', encoding='utf-8') as f:
            f.write('%s:%s:%d:%s:%s:%s\n' % (
                item['姓名'], item['备注'], item['性别'], item['省份'], item['城市'], item['个性签名'],))
  • 运行结果

       

2.朋友圈好友个签词云图

  • 步骤1:从txt文档提取个签项

​​​​​​​       定义一个gq[]列表,将上一步中生成的好友txt文档中好友个签那一项提取出来,因为存储顺序是姓名、备注、性别、省份、城市,个签,所以第5列是个签。

gq=[]
with open('friend.txt',mode='r',encoding='utf-8') as m:
    rows=m.readlines()
    for row in rows:
        try:
            gq.append(row.split(':')[5])
        except:
            continue
  • 步骤2:将提取的个签存入新的文档

​​​​​​​     因为有些好友个签中含表情,导致提取出来的个签是乱码或<span\......>格式,所以将含有‘<’的个签转换为空格。

for item in gq:
    if('<' in item):
        item=" "
    with open('个签1.txt', mode='a', encoding='utf-8') as f:
        f.write('%s\n' % (item))
  • 步骤3:将好友个签以词语形式提取并用词云图展示​​​​​​​
m=open('个签1.txt',mode='r',encoding='utf-8')
t=m.read()
ls=jieba.lcut(t)
m.close()
txt=" ".join(ls)
w=wordcloud.WordCloud(font_path="msyh.ttc",width=1000,height=700,max_words=25)
w.generate(txt)
w.to_file("个签1.png")
  •  效果展示

​​​​​​​                    

文章中部分代码与思路参考:https://blog.csdn.net/tangyang8941/article/details/82837284 

未完待续。。。。。。。 

猜你喜欢

转载自blog.csdn.net/qq_38290604/article/details/87198162