用Python控制安卓手机实现24小时美团抢菜,并成功抢到了

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

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --pre -U uiautomator2
adb:您可以转到[官方文档](https://developer.android.com/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__':
# 此处填设备编号
device_name = "b8c282ac"
run(device_name)
最后看一下成果:

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

 

再展示一下抢到的菜吧,同时祝大家早日抢到自己喜欢的菜吧,也希望疫情赶紧结束:(偷偷问问,鲍鱼怎么做,还是活的,,,,,,,)


————————————————
版权声明:本文为CSDN博主「1024小神」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44786530/article/details/124069742

posted @ 2022-04-10 10:40  愚哦  阅读(1878)  评论(0编辑  收藏  举报