如何用Python分析微信好友的性别和地域分布比例?

2026-05-25 01:421阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计463个文字,预计阅读时间需要2分钟。

如何用Python分析微信好友的性别和地域分布比例?

本示例讲述了Python分析微信好友性别和省份城市分布的方法。分享给家长供参考,内容如下:

安装itchat库:pip install itchat

使用itchat:from itchat.content import TEXT, FILEfrom itchat import start

def process_message(msg): if msg['Type']==TEXT: print(收到文本消息:, msg['Text']) elif msg['Type']==FILE: print(收到文件:, msg['FileName'])

start(process_message=process_message)

复制以下代码:pythonfrom itchat.content import TEXT, FILEfrom itchat import startimport pandas as pd

def process_message(msg): if msg['Type']==TEXT: print(收到文本消息:, msg['Text']) elif msg['Type']==FILE: if msg['FileName'].endswith('.csv'): with open(msg['FileName'], 'r', encoding='utf-8') as f: data=pd.read_csv(f) print(好友性别比例:) print(data['Gender'].value_counts(normalize=True)) print(好友省份城市分布:) print(data['Province'].value_counts()) print(data['City'].value_counts())

start(process_message=process_message)

将上述代码保存为`wxfx.py`文件,并运行:python wxfx.py

等待微信消息中包含好友信息的CSV文件,分析结果将自动输出。

本文实例讲述了Python分析微信好友性别比例和省份城市分布比例的方法。分享给大家供大家参考,具体如下:

安装itchat

pip install itchat

使用

新建wxfx.py,拷贝以下代码

# -*- coding: utf-8 -*- #导入模块 from wxpy import * ''' 微信机器人登录有3种模式, (1)极简模式:robot = Bot() (2)终端模式:robot = Bot(console_qr=True) (3)缓存模式(可保持登录状态):robot = Bot(cache_path=True) ''' #初始化机器人,选择缓存模式(扫码)登录 robot = Bot(cache_path=True) #获取好友、群、公众号信息 robot.chats() #获取好友的统计信息 Friends = robot.friends() print(Friends.stats_text())

运行

CMD->CD到wxfx.py所在目录运行,然后扫码登录

结果

如何用Python分析微信好友的性别和地域分布比例?

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

本文共计463个文字,预计阅读时间需要2分钟。

如何用Python分析微信好友的性别和地域分布比例?

本示例讲述了Python分析微信好友性别和省份城市分布的方法。分享给家长供参考,内容如下:

安装itchat库:pip install itchat

使用itchat:from itchat.content import TEXT, FILEfrom itchat import start

def process_message(msg): if msg['Type']==TEXT: print(收到文本消息:, msg['Text']) elif msg['Type']==FILE: print(收到文件:, msg['FileName'])

start(process_message=process_message)

复制以下代码:pythonfrom itchat.content import TEXT, FILEfrom itchat import startimport pandas as pd

def process_message(msg): if msg['Type']==TEXT: print(收到文本消息:, msg['Text']) elif msg['Type']==FILE: if msg['FileName'].endswith('.csv'): with open(msg['FileName'], 'r', encoding='utf-8') as f: data=pd.read_csv(f) print(好友性别比例:) print(data['Gender'].value_counts(normalize=True)) print(好友省份城市分布:) print(data['Province'].value_counts()) print(data['City'].value_counts())

start(process_message=process_message)

将上述代码保存为`wxfx.py`文件,并运行:python wxfx.py

等待微信消息中包含好友信息的CSV文件,分析结果将自动输出。

本文实例讲述了Python分析微信好友性别比例和省份城市分布比例的方法。分享给大家供大家参考,具体如下:

安装itchat

pip install itchat

使用

新建wxfx.py,拷贝以下代码

# -*- coding: utf-8 -*- #导入模块 from wxpy import * ''' 微信机器人登录有3种模式, (1)极简模式:robot = Bot() (2)终端模式:robot = Bot(console_qr=True) (3)缓存模式(可保持登录状态):robot = Bot(cache_path=True) ''' #初始化机器人,选择缓存模式(扫码)登录 robot = Bot(cache_path=True) #获取好友、群、公众号信息 robot.chats() #获取好友的统计信息 Friends = robot.friends() print(Friends.stats_text())

运行

CMD->CD到wxfx.py所在目录运行,然后扫码登录

结果

如何用Python分析微信好友的性别和地域分布比例?

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。