Py学习  »  Python

Python 分析微信朋友代码

数据分析小助手 • 5 年前 • 430 次点击  

微信公众号:数据分析联盟


通过Python我们可以爬去微信好友的数据,分析很多有趣的数据。包括昵称、性别、地理位置、签名、头像等数据。通过这些可以分析朋友圈群体特征和关键词等。


环境:Python3+itchat

一、昵称、性别、地理位置、个性签名等数据

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = '数据分析侠'

# 导入itchat模块,操作微信个人号的接口
import itchat

# 获取数据
def get_data():
    # 扫描二维码登陆微信,实际上就是通过网页版微信登陆
    itchat.auto_login()
    # 获取所有好友信息
    friends = itchat.get_friends(update=True)  # 返回一个包含用户信息字典的列表
    return friends

#处理数据
def parse_data(data):
    friends = []
    for item in data[1:]:  # 第一个元素是自己,排除掉
        friend = {
            'NickName': item['NickName'],  # 昵称
            'RemarkName': item['RemarkName'],  # 备注名
            'Sex': item['Sex'],  # 性别:1男,2女,0未设置
            'Province': item['Province'],  # 省份
            'City': item['City'],  # 城市
            'Signature': item['Signature'].replace('\n', ' ').replace(',', ' '),  # 个性签名(处理签名内容换行的情况)
            'StarFriend': item['StarFriend'],  # 星标好友:1是,0否
            'ContactFlag': item['ContactFlag']  # 好友类型及权限:1和3好友,259和33027不让他看我的朋友圈,65539不看他的朋友圈,65795两项设置全禁止
        }
        print(friend)
        friends.append(friend)
    return friends


# 存储数据,存储到文本文件
def save_to_txt():
    friends = parse_data(get_data())
    for item in friends:
        with open('friends.txt', mode='a', encoding='utf-8') as f:
            f.write('%s,%s,%d,%s,%s,%s,%d,%d\n' % (
                item['NickName'], item['RemarkName'], item['Sex'], item['Province'], item['City'], item['Signature'],
                item['StarFriend'], item['ContactFlag']))


if __name__ == '__main__':
    save_to_txt()


二、头像数据

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = '数据分析侠'

import itchat
import os
import math
from PIL import Image

# 获取头像数据
def get_image():
    itchat.auto_login()
    friends = itchat.get_friends(update=True)

    #  在当前位置创建一个用于存储头像的目录headImages
    base_path = 'headImages1'
    if not os.path.exists(base_path):
        os.mkdir(base_path)

    # 获取所有好友头像
    for friend in friends:
        img_data = itchat.get_head_img(userName=friend['UserName'])  # 获取头像数据
        img_name = friend['RemarkName'] if friend['RemarkName'] != '' else friend['NickName']
        img_file = os.path.join(base_path, img_name + '.jpg')
        print(img_file)
        with open(img_file, 'wb') as file:
            file.write(img_data)

# 拼接头像
def join_image():
    base_path = 'test'
    files = os.listdir(base_path)
    each_size = int(math.sqrt(float(640 * 640) / len(files)))
    lines = int(640 / each_size)
    image = Image.new('RGB', (640, 640))
    x = 0
    y = 0
    for file_name in files:
        img = Image.open(os.path.join(base_path, file_name))
        img = img.resize((each_size, each_size), Image.ANTIALIAS)
        image.paste(img, (x * each_size, y * each_size))
        x += 1
        if x == lines:
            x = 0
            y += 1
    image.save('all.jpg')
    #itchat.send_image('all.jpg', 'filehelper')


if __name__ == '__main__':
    join_image()



今天看啥 - 高品质阅读平台
本文地址:http://www.jintiankansha.me/t/JgAAhC0etx
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/26115
 
430 次点击