2024年最新的Python操控微信教程

自从微信禁止网页版登陆之后,itchat 库实现的功能也就都不能用了,那现在 Python 还能操作微信吗?答案是:可以!

在Github上有一个项目叫《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(pull_type=1)
print(data)

# 获取群成员列表
data = w.get_chat_room_members(to_chat_room="xxx@chatroom")
print(data)

朋友圈操作

# 获取朋友圈数据
moments = w.get_moments()
if not moments:
    print("没有最新的朋友圈")
else:
    for item in moments:
        print(item)

# 朋友圈点赞/取消点赞
w.like_moments(
    moments_id="130000",
    state=True
)

# 朋友圈评论
w.comment_moments(
    moments_id="1300000",
    content="我是评论内容"
)

# 朋友圈删除评论
w.del_comment_moments(
    moments_id="1300000",
    comment_id="3"
)

发送、接收、转发消息

# 发送文本消息
w.send_text(to_wx="filehelper", msg='你好鸭~')
time.sleep(1)

# 发送图片消息
w.send_img(to_wx="filehelper", path=r"C:\Users\Administrator\Desktop\1.png")
time.sleep(1)

# 发送卡片链接
w.send_card_link(
    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(msg_data=msg)

    # 收款
    elif msg["msg_type"] == 490:
        is_recv = msg["detail"]["is_recv"]
        if is_recv:
            # 收款
            w.collection(msg_data=msg)

    # 保存图片
    elif msg["msg_type"] == 3:
        w.save_img(
            save_path=os.path.join(BASE_DIR, "temp\\1.png"),
            msg_data=msg
        )

    # 如果是老板发来的信息
    if msg["wx_id"] == "wxid_xxx":
        # 转发给工作小组
        w.forward_msg("xxxxxx@chatroom", msg["msg_id"])

以下是效果图:

  • 28
    点赞
  • 387
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
以下是 Python 调用微信支付的基本流程: 1. 创建订单:首先需要在微信支付后台创建订单并获取到订单号。 2. 生成签名:根据微信支付接口文档要求,需要对订单信息进行签名加密。 3. 统一下单:调用微信支付接口,将订单信息提交给微信支付后台,获取到预支付交易会话标识 prepay_id。 4. 生成支付参数:根据微信支付接口文档要求,生成支付参数并返回给前端。 5. 调起支付:前端使用生成的支付参数发起支付请求。 下面是一个简单的 Python 程序示例,用于调用微信支付: ```python import requests import hashlib import time import xml.etree.ElementTree as ET # 微信支付接口地址 url = 'https://api.mch.weixin.qq.com/pay/unifiedorder' # 微信支付相关信息 appid = '微信公众号appid' mch_id = '商户号' key = '商户支付密钥' body = '订单描述' out_trade_no = '商户订单号' total_fee = '订单金额,单位为分' notify_url = '支付成功后的回调地址' trade_type = 'JSAPI' openid = '用户openid' # 生成签名 timestamp = str(int(time.time())) nonce_str = 'wechat_pay' stringA = 'appid=' + appid + '&body=' + body + '&mch_id=' + mch_id + '&nonce_str=' + nonce_str + '&notify_url=' + notify_url + '&openid=' + openid + '&out_trade_no=' + out_trade_no + '&total_fee=' + total_fee + '&trade_type=' + trade_type + '&key=' + key sign = hashlib.md5(stringA.encode('utf-8')).hexdigest().upper() # 生成支付参数 xml_data = '<xml>' xml_data += '<appid>' + appid + '</appid>' xml_data += '<body>' + body + '</body>' xml_data += '<mch_id>' + mch_id + '</mch_id>' xml_data += '<nonce_str>' + nonce_str + '</nonce_str>' xml_data += '<notify_url>' + notify_url + '</notify_url>' xml_data += '<openid>' + openid + '</openid>' xml_data += '<out_trade_no>' + out_trade_no + '</out_trade_no>' xml_data += '<total_fee>' + total_fee + '</total_fee>' xml_data += '<trade_type>' + trade_type + '</trade_type>' xml_data += '<sign>' + sign + '</sign>' xml_data += '</xml>' # 调用微信支付接口 response = requests.post(url, data=xml_data.encode('utf-8')) response.encoding = 'utf-8' # 解析返回结果 tree = ET.fromstring(response.text) prepay_id = tree.find('prepay_id').text # 生成支付参数 timestamp = str(int(time.time())) nonce_str = 'wechat_pay' stringA = 'appId=' + appid + '&nonceStr=' + nonce_str + '&package=prepay_id=' + prepay_id + '&signType=MD5' + '&timeStamp=' + timestamp + '&key=' + key pay_sign = hashlib.md5(stringA.encode('utf-8')).hexdigest().upper() pay_param = { 'appId': appid, 'timeStamp': timestamp, 'nonceStr': nonce_str, 'package': 'prepay_id=' + prepay_id, 'signType': 'MD5', 'paySign': pay_sign } # 返回支付参数 print(pay_param) ``` 以上代码仅供参考,实际调用微信支付时还需要根据接口文档和具体业务需求进行调整。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值