python练习之安装itchat以及分析微信好友信息

安装itchat包:

  • 方法一:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    此操作较为麻烦
  • 方法二:

在这里插入图片描述

[root@westos_netfilesystem day02]# pip3 install itchat

使用此命令安装比较慢:
在这里插入图片描述
建议指定由国内的 http://pypi.douban.com/simple安装(此为国内豆瓣提供的镜像源下载itchat包),使用该方法安装非常快:

[root@westos_netfilesystem day02]# pip3 install itchat -i http://pypi.douban.com/simple

在这里插入图片描述

1.分析微信好友信息:

# Alt + Insert  ==== Create New file
# Ctrl + Alt + S  ===== seetings
# python package: itchat
import itchat

# 1. Login weixin
itchat.auto_login(hotReload=True)

# 2. get friends information, return friends List [1, 2, 3, 4, .....]
# Every friend information saved as dict.
friends = itchat.get_friends()

# First friend information is yours
mine = friends[0]
name = mine['NickName']
Signature = mine['Signature']

# init male, female, other = 0
male = female = other = 0
for friend in friends[1:]:
    sex = friend['Sex']
    if sex == 1:
        male += 1
    elif sex == 2:
        female += 1
    else:
        other += 1
# Ctrl + D : Quick copy one line
print("""
*******************************weixin INformation***********************
        Name: %s
        Signature: %s
        Male friends count: %d 
        Female friends count: %d
        Unknown sex friends count: %d 

""" %(name, Signature, male, female, other))

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.分析微信好友省份信息

# 1. Analyse
# friend province
# "shanxi"-200  "beijing"-100  "shandong" 10 .........
# dict: {"province_name":people_num}

# 2.
import itchat
from collections import  Counter

# 3.
itchat.auto_login(hotReload=True)
# friends===List, friend===Dict {"NickName":"", "Sex":"", "Province":""}
# First Information is your info
friends = itchat.get_friends()

# First friend information is yours
mine = friends[0]
name = mine['NickName']
Signature = mine['Signature']


# 4.
# Save Province to dict
provinces = {}
for friend in friends[1:]:
    province = friend['Province']
    if province != '':
        # If province not in dict, set value=1
        # If province in dict, value += 1
        if province in provinces:
            # value = provinces[province]    # province people count
            # value += 1                     # value + 1
            # provinces[province] = value     # set new value
            provinces[province]  += 1
        else:
            provinces[province] = 1

# 5.
counter = Counter(provinces)
top_5_provinces = counter.most_common(5)

# 6.
print("""
*******************************weixin INformation***********************
        Name: %s
        Signature: %s
""" %(name, Signature))

print("*******************************weixin friends Province***********************")
for name, count in top_5_provinces:
    print("%s : %s" %(name, count))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用微信官方提供的接口实现给指定微信好友发送消息,具体步骤如下: 1. 首先,需要在微信公众平台上申请开发者账号,并创建一个公众号。 2. 在公众号后台,开启开发者模式,并获取相应的 AppID 和 AppSecret。 3. 通过微信官方提供的 API,获取 access_token,用于发送消息和其他操作。 4. 调用微信官方提供的接口,向指定好友发送消息。 以下是一个简单的 Python 示例代码: ```python import requests # 获取 access_token def get_access_token(app_id, app_secret): url = f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={app_id}&secret={app_secret}" response = requests.get(url) access_token = response.json().get("access_token") return access_token # 发送消息 def send_message(access_token, to_user, content): url = f"https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={access_token}" data = { "touser": to_user, "msgtype": "text", "text": { "content": content } } response = requests.post(url, json=data) result = response.json() if result.get("errcode") == 0: print("消息发送成功!") else: print("消息发送失败!") if __name__ == "__main__": # 替换成自己的 AppID 和 AppSecret app_id = "your_app_id" app_secret = "your_app_secret" # 替换成自己的 access_token access_token = get_access_token(app_id, app_secret) # 替换成自己的好友 openid 和消息内容 to_user = "your_friend_openid" content = "Hello, World!" # 发送消息 send_message(access_token, to_user, content) ``` 需要注意的是,由于微信官方对接口的访问频率有限制,建议在发送消息之前,先检查当前 access_token 是否有效,如果无效,则重新获取。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值