应用思考-教育技术论坛

标题: 使用python抢菜 [打印本页]

作者: etthink    时间: 2022-4-10 17:42
标题: 使用python抢菜
网上转载,未验证是否可以用电脑程序运行手机中的软件。
具体方法如下:
一.安装环境
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)






欢迎光临 应用思考-教育技术论坛 (http://www.etthink.com/) Powered by Discuz! X3.4