分享

用Pyhton写了个抢菜自动化程序

 吕居居 2022-04-10

shanghai 现在抢菜之苦,真的有口说不出,每天早上6点,6点半,8点半,体验过的人的都知道,即便手速再快,还是抢不到.....

然后就有了下面的抢菜程序,完全由Python实现,并会附上教程:

一.安装环境

python3:安装python3环境,并安装依赖:

pip install -i https://pypi.tuna./simple --pre -U uiautomator2

adb:您可以转到[官方文档](https://developer./studio/command-line/adb

( mac电脑adb安装:brew install android-platform-tools )

手机:安卓手机一部或者多部,或者安卓虚拟机一个或者多个

二.调试环境

adb:adb安装成功后,在命令行输入adb,会出现以下提示:

adb start-server:启动服务

adb devices:查看连接的设备列表

当安卓手机和电脑连接之后,再输入:adb devices,就可以看到设备编号,记住这个编号,后面有用。

(如果用的安卓虚拟机,需要注意:需要先执行:adb kill-server,然后再执行:adb start-server,再执行:adb devices,才会显示设备列表)

设备上安装atx-agent:

python -m uiautomator2 init

会在安卓设备上安装一个依赖ATX软件:

三.开始抢菜

打开美团,并进入购物车页面,确认购物车里是有商品的:(没有商品肯定是不行的,对吧)

然后开始运行代码:

複製代碼


import os
import time
import uiautomator2 as u2


# 连接手机
def connect_phone(device_name):
    d = u2.connect(device_name)
    if not d.service(\\"uiautomator\\").running():
        # 启动uiautomator服务
        print(\\"start uiautomator\\")
        d.service(\\"uiautomator\\").start()
        time.sleep(2)

    if not d.agent_alive:
        print(\\"agent_alive is false\\")
        u2.connect()
        d = u2.connect(device_name)
    return d


def run(device_name):
    d = connect_phone(device_name)
    d.app_start(\\"com.sankuai.meituan\\")
    count = 1
    time_start = time.time()

    while True:
        start = time.time()
        if d(textContains=\\"结算(\\").exists:
            print(\\"点击结算\\")
            d(textContains=\\"结算(\\").click()

        if d(text=\\"我知道了\\").exists:
            print(\\"点击我知道了\\")
            d(text=\\"我知道了\\").click()

        if d(text=\\"返回购物车\\").exists:
            print(\\"点击返回购物车\\")
            d(text=\\"返回购物车\\").click()

        if d(text=\\"立即支付\\").exists:
            print(\\"点击立即支付\\")
            d(text=\\"立即支付\\").click()

        if d(text=\\"确认并支付\\").exists:
            print(\\"点击确认并支付\\")
            d(text=\\"确认并支付\\").click()

        if d(resourceId=\\"btn-line\\").exists:
            print(\\"确认支付\\")
            d(resourceId=\\"btn-line\\").click()
            # mac系统使用语音提示:说抢到菜了,windows请屏蔽
            # os.system('say \\"抢到菜了,快来看\\"')
            break
        print(\\"本次花费时间:\\", time.time() - start)
        print(\\"总共花费时间:\\", (time.time() - time_start) / 60, \\"分钟,第\\", count, \\"次\\")
        count += 1


if __name__ == '__main__':
    # 此处填设备编号:由1024我的小表妹原创
    device_name = \\"b8c282ac\\"
    run(device_name)

最后看一下成果:

(因为我有两个安卓手机,又另外开了一个安卓虚拟机,所以就是下面效果了,哈哈哈哈哈)

再展示一下抢到的菜吧,同时祝大家早日抢到自己喜欢的菜吧,也希望疫情赶紧结束:

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多