自从微信禁止网页版登陆之后,itchat 库实现的功能也就都不能用了,那现在 Python 还能操作微信吗?答案是:还可以!
目前有一个项目《WeChatPYAPI》可以使用 Python 对微信进行操控。简单来说,它是通过逆向PC版微信客户端来实现对微信进行操控,使用Python封装后使用更加简单。
GitHub地址:https://github.com/mrsanshui/WeChatPYAPI
码云地址:https://gitee.com/mrsanshui/WeChatPYAPI
拉取(好友/群/公众号)等列表
# 拉取列表(好友/群/公众号等)
# 好友列表:pull_type = 1
# 群列表:pull_type = 2
# 公众号列表:pull_type = 3
# 其他:pull_type = 4
data = w.pull_list(self_wx="wxid_xxx", pull_type=1)
print(data)
# 获取群成员列表
data = w.get_chat_room_members(self_wx="wxid_xxx", to_chat_room="xxx@chatroom")
print(data)
拉取企业微信列表
# 拉取企业微信列表(好友/群)
data = w.pull_list_of_work(self_wx="wxid_xxx")
print(data)
# 获取企业群成员列表
data = w.get_chat_room_members_of_work(self_wx="wxid_xxx", to_chat_room="xxx@im.chatroom")
print(data)
发送、接受消息
# 发送文本消息
w.send_text(self_wx=self_wx, to_wx="filehelper", msg='你好鸭~')
time.sleep(1)
# 发送图片消息
w.send_img(self_wx=self_wx, to_wx="filehelper", path=r"C:\Users\Administrator\Desktop\1.png")
time.sleep(1)
# 发送卡片链接
w.send_card_link(
self_wx=self_wx,
to_wx="filehelper",
title="我是卡片标题",
desc="我是卡片描述啊啊啊啊啊啊啊啊啊啊",
target_url="http://baidu.com",
img_url="http://img.czdsh.com/Fsc_C6Rz5Sk7sblr_Q4YI0Y9v0zb"
)
# 处理消息回调
while True:
msg = msg_queue.get()
if msg["msg_type"] == 37:
# 同意添加好友申请
w.agree_friend(self_wx=self_wx, msg_data=msg)
# 收款
elif msg["msg_type"] == 490:
is_recv = msg["detail"]["is_recv"]
if is_recv:
# 收款
w.collection(self_wx=self_wx, msg_data=msg)
# 退款
# w.refund(self_wx=self_wx, msg_data=msg)
# 保存图片
elif msg["msg_type"] == 3:
w.save_img(
self_wx=self_wx,
save_path=os.path.join(BASE_DIR, "temp\\1.png"),
msg_data=msg
)
# 同意好友邀请进群
elif msg["msg_type"] == 491:
w.agree_friend_invite_join_chat_room(self_wx=self_wx, msg_data=msg)