wxpy
微信机器人 / 可能是最优雅的微信个人号 API
wxpy 在 itchat 的基础上,通过大量接口优化提升了模块的易用性,并进行丰富的功能扩展
官方文档
(官方文档讲得很全且很清晰,是入门和系统学习的最佳资料)
个人学习记录的基础代码,包含了好友信息获取,消息发送。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/12/18 9:45
# @Author : ystraw
# @Site :
# @File : tryAtry.py
# @Software: PyCharm Community Edition
# @function: 练习使用wxpy
# 导入模块
from wxpy import *
# 初始化机器人,扫码登陆
bot = Bot()
# 获取整个好友列表:
my_friend = bot.friends()
# 获取第一个好友对象
first_friend = my_friend[0]
# 查看他的名字和城市
print(my_friend[0].name)
print(my_friend[0].city)
# 搜索名称含有 "于" 的男性好友
my_friend2 = bot.friends().search('于', sex=MALE)[0] # search里面可以传递更多的参数精确查找
print(my_friend2)
# 发送消息:
# 发送文本给好友
my_friend2.send('Hello WeChat!')
# 发送图片
my_friend2.send_image('./images/test.png')
# 保持登陆/运行:
# 进入 Python 命令行、让程序保持运行
embed()
# 或者仅仅堵塞线程
# bot.join()