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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM