使用Python操作微信的示例分析
发表于:2024-10-22 作者:千家信息网编辑
千家信息网最后更新 2024年10月22日,这篇文章将为大家详细讲解有关使用Python操作微信的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。仅供学习参考使用获取好友列表WechatPCAPI 提供
千家信息网最后更新 2024年10月22日使用Python操作微信的示例分析
这篇文章将为大家详细讲解有关使用Python操作微信的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
获取好友列表
WechatPCAPI
提供了方法 get_friends()
,该方法返回信息包括:好友、群和公众号的列表信息,信息内容主要包括:微信号、昵称和自己设置的备注。
我们使用获取的昵称做个简单的词云展示,代码实现如下所示:
logging.basicConfig(level=logging.INFO)
def on_message(message):
pass
def get_friends():
# 初始化微信实例
wx_inst = WechatPCAPI(on_message=on_message, log=logging)
# 启动微信
wx_inst.start_wechat(block=True)
# 等待登陆成功,此时需要人为扫码登录微信
while not wx_inst.get_myself():
time.sleep(5)
print('登陆成功')
nicknames = []
# 排除的词
remove = ['还是', '不会', '一些', '所以', '果然',
'起来', '东西', '为什么', '真的', '这么',
'但是', '怎么', '还是', '时候', '一个',
'什么', '自己', '一切', '样子', '一样',
'没有', '不是', '一种', '这个', '为了'
]
for key, value in wx_inst.get_friends().items():
if key in ['fmessage', 'floatbottle', 'filehelper'] or 'chatroom' in key:
continue
nicknames.append(value['wx_nickname'])
words = []
for text in nicknames:
if not text:
continue
for t in jieba.cut(text):
if t in remove:
continue
words.append(t)
global word_cloud
# 用逗号隔开词语
word_cloud = ','.join(words)
def nk_cloud():
# 打开词云背景图
cloud_mask = np.array(Image.open('bg.png'))
# 定义词云的一些属性
wc = WordCloud(
# 背景图分割颜色为白色
background_color='white',
# 背景图样
mask=cloud_mask,
# 显示最大词数
max_words=300,
# 显示中文
font_path='./fonts/simkai.ttf',
# 最大尺寸
max_font_size=70
)
global word_cloud
# 词云函数
x = wc.generate(word_cloud)
# 生成词云图片
image = x.to_image()
# 展示词云图片
image.show()
# 保存词云图片
wc.to_file('nk.png')
看一下效果:
消息防撤回
我们在使用微信和好友聊天时,对方有时会有撤回消息的情况,正常情况下,我们是不知道好友撤回的消息是什么的,通过 WechatPCAPI
就可以实现消息防撤回的功能。
我们知道通常撤回的消息是点击撤回操作前一步发送的内容,当然也可能撤回的是前两步、三步 ... 的消息,这里我们只对撤回前一步的消息做处理,基本思路是:我们将撤回前一步发送的消息存一下,当对方点击撤回操作时,我们再将前一步的消息再次返回给自己。
下面看一下实现代码:
logging.basicConfig(level=logging.INFO)
queue_recved_event = Queue()
def on_message(msg):
queue_recved_event.put(msg)
def login():
pre_msg = ''
# 初始化微信实例
wx_inst = WechatPCAPI(on_message=on_message, log=logging)
# 启动微信
wx_inst.start_wechat(block=True)
# 等待登陆成功,此时需要人为扫码登录微信
while not wx_inst.get_myself():
time.sleep(5)
print('登陆成功')
while True:
msg = queue_recved_event.get()
data = msg.get('data')
sendinfo = data.get('sendinfo')
data_type = str(data.get('data_type'))
msgcontent = str(data.get('msgcontent'))
is_recv = data.get('is_recv')
print(msg)
if data_type == '1' and 'revokemsg' not in msgcontent:
pre_msg = msgcontent
if sendinfo is not None and 'revokemsg' in msgcontent:
user = str(sendinfo.get('wx_id_search'))
recall = '撤回的消息:' + pre_msg
wx_inst.send_text(to_user=user, msg=recall)
看一下操作效果:
关于"使用Python操作微信的示例分析"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
消息
成功
好友
登陆
信息
内容
图片
篇文章
背景
示例
分析
最大
代码
实例
对方
情况
效果
方法
昵称
更多
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库设计联系实体
南昌惠联网络技术有限公司夏孟槐
黑客攻防与网络安全试题
5万元软件开发费入什么科目
坐标怎么存在数据库中
海康综合平台管理服务器
软件开发的用户故事实例
数据库约束dbs有哪些
嵌入式软件开发可以考研吗
mysql数据库1072
嵌入式应用软件开发阶段
网络安全社会实践总结
网络安全运维和软件开发
新网络技术有哪些
网络安全班会发言稿
提升网络安全方面的建议
博客备份软件开发
信阳软件开发解决方案
潼南区网络安全审计系统咨询辅导
孤岛惊魂6服务器机房在哪
软件开发用电脑推荐
毕节网络安全公司
hp刀箱管理服务器地址
向服务器发送请求失败怎么处理
开源本地数据库表格
阿里云 建立数据库
docker里连接不到数据库
网络技术与应用是做什么用的
数据库主主复制
三校生计算机网络技术高考