airtest爬取app内容

案例1:爬取知乎app数据

# -*- encoding=utf8 -*-
from airtest.core.api import *
from airtest.core.android.android import Android
auto_setup(__file__)
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco=AndroidUiautomationPoco(force_restart=False)
connect_device('Android:///')
poco(name='com.zhihu.android:id/input').click()
sleep(3)
poco(name='com.zhihu.android:id/input').set_text('追风筝的人')
sleep(4)

title_obj_list = poco(name='com.zhihu.android:id/magi_title')#行的元素(内容行)
title_list = [title.get_text() for title in title_obj_list]#迭代title_obj_list元素的个数(也教遍历)
#print(title_list)
discuss_obj_list = poco(name='com.zhihu.android:id/magi_count')#行对应的讨论数
discuss_list = [discuss.get_text() for discuss in discuss_obj_list]#遍历discuss_obj_list
#遍历格式化
for title, discuss in zip(title_list, discuss_list):
    print(title, discuss)
exit()

 

案例二:爬取拼多多数据

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from airtest.core.api import *
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
auto_setup(__file__)
#连接设备
connect_device("Android://127.0.0.1:5037/ea91a8e0")
#初始化设备
poco=AndroidUiautomationPoco()
#这个元素可能会变(根据时间情况调整)
node_my=poco("com.xunmeng.pinduoduo:id/asr").offspring("com.xunmeng.pinduoduo:id/caz").offspring("com.xunmeng.pinduoduo:id/dja").children().offspring("com.xunmeng.pinduoduo:id/f7z")
##______________________遍历获取元素的text形成list__________________________
# list=[j.get_text() for j in node_my]
# print(list)

##___________________依次点击菜单_并获取元素____________________________________
category=[]
for i in node_my:
    print(i.get_text())
    category.append(i.get_text())
    i.click()
    sleep(1)
    
print('当前页面类目有:',category)

 

posted on 2019-05-14 18:06  chen_2987  阅读(2135)  评论(0编辑  收藏  举报

导航