当前位置:首页 » 《随便一记》 » 正文

UiAutomator2+weditor+python获取页面(android)文本数据_WarmPure

3 人参与  2021年11月26日 14:23  分类 : 《随便一记》  评论

点击全文阅读


终极目标:获取抖音直播时评论数据。

准备工作:

1. android手机或模拟器(无需root)

2. python环境

开始:

1.设备连接

android手机打开开发者模式,USB连接电脑,允许USB调试。

命令行输入adb devices查看设备,AWMCUT1224008813为设备序列号待会要用

>adb devices
List of devices attached
AWMCUT1224008813  device

2.安装UiAutomator2

确保python环境没有问题,我这里使用的python3

命令行运行pip3 install -U uiautomator2 安装UiAutomator2

3.安装httprpc服务

运行python -m uiautomator2 init安装包含httprpc服务的apk到手机

上述初始化命令会自动安装本库所需要的设备端程序:uiautomator-server、atx-agent、openstf/minicap、openstf/minitouch,并且在被测手机终端上会安装一个名为ATX的代理程序

4.安装weditor

运行pip3 install --pre -U weditor安装weditor在线元素查看器,weditor --help查看是否安装成功

5. 使用weditor查看界面元素

命令行执行 weditor 会自动打开浏览器(http://localhost:17310/),输入设备的ip或者序列号,点击Connect,打开实时开关。

 此时打开app,可以在右侧Hierarchy查看界面元素树

 6.编写脚本获取评论列表文本内容

d = u2.connect('AWMCUT1224008813') 对应连接设备的序列号,

#coding=utf8
import uiautomator2 as u2
import time
import logging
import re
import json
from string import Template
d = u2.connect('AWMCUT1224008813')
d.click_post_delay = 1.5#
 
logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(funcName)s - %(message)s')
logger = logging.getLogger(__name__)
logger.info(d)

def run_app():
    d.app_start("com.ss.android.ugc.aweme")#打开程序
    if d.session("com.ss.android.ugc.aweme", attach=True):#判断程序是否打开
        logger.info("已打开程序")
        if d(text="更多直播").exists(timeout=5):#exists:等待元素出现
            logger.info("当前是打开状态״̬")
      
            print("size:", d.xpath('//*[@resource-id="com.ss.android.ugc.aweme:id/fvw"]/android.widget.RelativeLayout').all())
            
            for index,elem in enumerate(d.xpath('//*[@resource-id="com.ss.android.ugc.aweme:id/fvw"]/android.widget.RelativeLayout').all()): 
                print("%s" %(index+1))
                i = index + 1
                s = f"//android.widget.RelativeLayout[{i}]/android.widget.TextView"
                print(s)
                print("item:", d.xpath(s).text)
                
        else:
            logger.info("未找到指定文本״̬")
    else:
        print('app打开错误')
        

        
run_app()

部分log如下:

分类: 美食探店
1
//android.widget.RelativeLayout[1]/android.widget.TextView
item: 说点什么...
2
//android.widget.RelativeLayout[2]/android.widget.TextView
item: $ 楠楠楠:店铺
3
//android.widget.RelativeLayout[3]/android.widget.TextView
item: $ 锁锁美:4号代金券限菜品吗使用吗?
4
//android.widget.RelativeLayout[4]/android.widget.TextView
item: $ 小熙:怎么适应
5
//android.widget.RelativeLayout[5]/android.widget.TextView
item: $ 小熙:怎么使用
6
//android.widget.RelativeLayout[6]/android.widget.TextView
item: 耶啵啵:那为什么显示生菜一份 两人需要再付一份?
7
//android.widget.RelativeLayout[7]/android.widget.TextView
item: Joker男士理发馆Jimmy 来了
[Finished 9.701s]

这段脚本执行后会根据包名自动打开抖音,在直播页面通过d.xpath('//*[@resource-id="com.ss.android.ugc.aweme:id/fvw"]')获取评论列表的recyclerview,再通过遍历recyclerview下的RelativeLayout打印出RelativeLayout下的TextView中内容。当然也可以直接遍历TextView的id(text)来进行获取,注意:不同设备的ViewGroup的id也就是TextView的父view的id可能会不同。

对列表元素的遍历是从1开始的。

另外,此时仅获取到当前屏幕上的数据,可以加个定时器不断的进行获取并保存,再对数据去重。


点击全文阅读


本文链接:http://zhangshiyu.com/post/31294.html

打开  安装  设备  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1