xxxxxxxxxxxxxxxxxxx

1.

# -*- encoding=utf8 -*-
__author__ = "1003441"

from airtest.core.api import *

auto_setup(__file__)
# 日志模块
from airtest.report.report import simple_report

simple_report(__file__)
# 连接设备,初始化界面ui
dev = connect_device("Android://127.0.0.1:5037/127.0.0.1:21523")
from poco.drivers.android.uiautomation import AndroidUiautomationPoco

poco = AndroidUiautomationPoco(dev, use_airtest_input=True, screenshot_each_action=False)


def start_aikucun():
    """
    启动app并处理一些广告和弹窗提示
    每次运行之前都要把确保app是刚打开的页面
    :return:
    """
    stop_app("com.aikucun.akapp")
    start_app("com.aikucun.akapp")
    # 这边需要管理这些潜在的广告界面
    # 启动的时候可能存在一些广告页面
    # todo优化成等到首页的某个元素出现比较保险,不要用time.sleep()
    time.sleep(15)
    if poco(name='com.aikucun.akapp:id/dialog_image_show').exists():
        poco(name='com.aikucun.akapp:id/dialog_close').click()  # 关闭广告按钮

    elif poco(name='com.aikucun.akapp:id/notice_style_close').exists():
        poco(name='com.aikucun.akapp:id/notice_style_close').click()  # 确定按钮关闭 弹窗


def get_detail():
    """
    详情页的滑动和品牌切换
    滑动获取数据,数据获取完,直接在当前页切换到下一个品牌,重复滑动操作
    :return:
    """
    global now_brand
    while True:
        poco.swipe([0.5, 0.85], [0.5, 0.2], duration=0.1)
        if poco(text='已加载完毕').exists():
            break
        time.sleep(0.5)
    # 切换品牌,这边要判断是否已经是最后几个品牌了
    poco(name='com.aikucun.akapp:id/tv_title').click()
    # 等待品牌列表弹出来(选择品牌标签)
    poco(name='com.aikucun.akapp:id/cancel_layout_tv').wait_for_appearance()
    # 保留当前的品牌名称
    now_brand = poco("android.widget.FrameLayout").offspring("com.aikucun.akapp:id/ll_dialog_indefinite") 
        .offspring("com.aikucun.akapp:id/recycleView").child("android.widget.LinearLayout") 
        [1].child("com.aikucun.akapp:id/brand_layout").child('com.aikucun.akapp:id/brand_name_tv').get_text()
    print('当前品牌是:%s' % now_brand)

    # 点击进入品牌
    poco("android.widget.FrameLayout").offspring("com.aikucun.akapp:id/ll_dialog_indefinite") 
        .offspring("com.aikucun.akapp:id/recycleView").child("android.widget.LinearLayout")[1].wait(6).click()

    # 切换品牌的时候,有的时候已进入品牌并不会有数据,保险的方法就是切换到一个新的品牌的时候都刷新一下页面
    time.sleep(0.5)
    poco.swipe([0.5, 0.4], [0.5, 0.95], duration=1)
    time.sleep(1)
    if poco(name='com.aikucun.akapp:id/headImage').exists():
        pass
    else:
        poco.swipe([0.5, 0.5], [0.5, 0.95], duration=3)
        time.sleep(1.5)


def clear_and_restart():
    """
    定期关闭app在重启,清理内存 ,防止内存溢出
    重新启动爱库存,并搜索上一轮抓取的最后一个品牌
    :return:
    """
    print('抓了90个,重启一下app')
    stop_app('com.aikucun.akapp')
    #     poco(text='爱库存').wait_for_appearance()
    start_aikucun()
    poco(name='com.aikucun.akapp:id/btn_left').wait_for_appearance()
    poco(name='com.aikucun.akapp:id/btn_left').wait(10).click()
    poco(name='com.aikucun.akapp:id/search_live_et').wait_for_appearance()
    poco(name='com.aikucun.akapp:id/search_live_et').set_text(now_brand)
    # 搜索到品牌之后就点击进入品牌详情,重复上滑动作
    try:
        poco(name='com.aikucun.akapp:id/brand_name_tv').wait(10).click()
        get_detail()
    except Exception as e:
        clear_and_restart()


def get_data():
    """
    点击品牌列表进入详情页并滑动详情页的所有数据
    :return:
    """
    poco(text='品牌').wait(15).click()
    # 获取当前时间点的品牌总数量,用来计数的
    brand_count = poco(name='com.aikucun.akapp:id/brand_count').get_text()
    brand_count = int(brand_count)
    print('当前时间共有%d个品牌' % brand_count)

    # 点击品牌列表的第一个品牌(入口),并且等待品牌列表出现 才继续下一步
    poco(name='com.aikucun.akapp:id/brand_name_tv').wait(15).click()
    count = 0
    while True:
        if count <= brand_count - 4:
            get_detail()
            count += 1
            print('第%d个品牌抓取完成' % count)
            # 定期清理一下内存,重新进入app,重复执行之前的动作
            if count % 50 == 0:
                time.sleep(120)
            elif count % 90 == 0:
                clear_and_restart()
        else:
            break


def run():
    try:
        start_aikucun()
        get_data()
    except Exception as e:
        clear_and_restart()


if __name__ == '__main__':
    run()

# todo异常处理
#     if poco(name='android:id/message',textMatches='^很抱歉,“爱库存”已停止运行。.*$').exists()
#         poco(name='android:id/button1',textMatches='^确定.*$').click()

2.

原文地址:https://www.cnblogs.com/tjp40922/p/14753380.html