利用Python操作Android手機刷抖音

前言:

很多時候圣贸,想要學習一個陌生領(lǐng)域的知識時殴泰,需要先定一個或基礎(chǔ)或高階的目的于宙,這個目標最好帶有趣味性,然后錨定這個目標悍汛;或許最終目標很輕松就被實現(xiàn)了捞魁,也或許最終發(fā)現(xiàn)目標實在太高難以實現(xiàn);這些都沒有關(guān)系离咐,一步步探索谱俭,一步步走進目標的過程并在這個過程中得到提升才是學習的真正意義所在。

在一步步試探的過程中宵蛀,我們會思考昆著、搜索資料、質(zhì)疑是否有更好的實現(xiàn)方式糖埋;這一路的過程可以概括為:

——了解它

——走近它

——觀察它/測試它

——改造它/思考測試

——創(chuàng)造另一個它

只要自己不停止研究宣吱,目標只會越來越近窃这;正所謂:日拱一卒瞳别,功不唐捐是也征候。

一、下載安裝python

https://www.python.org/downloads/windows/

根據(jù)自己的OS版本選擇安裝

二祟敛、安裝uiautomator2包

安裝uiautomator2

pip install uiautomator2 -i https://pypi.douban.com/simple/</pre>

或者(建議在virtualenv下操作)

pip install --pre -U uiautomator2

需要較長時間0贪印!9萏跑揉!

三、安裝ADB工具

ADB下載路徑

Windows版本:https://dl.google.com/android/repository/platform-tools-latest-windows.zip

Mac版本:https://dl.google.com/android/repository/platform-tools-latest-darwin.zip

Linux版本:https://dl.google.com/android/repository/platform-tools-latest-linux.zip

將文件下載下來埠巨,解壓縮到自定義的安裝目錄即可历谍。

也可以直接安裝Android SDK;在Android SDK安裝目錄下的platform-tools文件夾中可以找到adb.exe程序辣垒。

四望侈、對移動設備執(zhí)行初始化

首先確保

1.手機設備成功接通到電腦(USB或WIFI);

2.手機開啟了調(diào)試模式勋桶;

以下幾項權(quán)限比較重要:

——開啟開發(fā)者模式(必要)

——USB調(diào)試(必要)

——USB安裝(必要)

——USB模擬點擊(必要)

——指針位置(建議開啟脱衙,非強制)

沒開啟前,我們再電腦上使用adb devices看到的結(jié)果是這樣的:

image-20221022092436321.png

3.電腦上安裝adb工具(包含在Android SDK中例驹,也可以單獨安裝)捐韩;建議架構(gòu)adb工具為位置加入環(huán)境變量PATH

注意:下圖的顯示是根據(jù)你手機啟用的調(diào)試模式顯示的

USB調(diào)試是這樣:


image-20221022093355511.png

對移動設備初始化

這個是最重要的操作。

這個過程會在移動設備上鹃锈,安裝一個agent(需要手機調(diào)試模式中允許調(diào)試安裝app)

python -m uiautomator2 init

如果提示(這個很重要荤胁,事個常見報錯)

ModuleNotFoundError: No module named 'dataclasses'

就安裝這個庫

pip install dataclasses

再次運行就可以看到安裝成功了:(有些手機需要插入SIM卡才可以開啟調(diào)試模式下安裝軟件的功能;根據(jù)要求滿足開啟條件就行屎债。)

(myenv) PS D:\xiamingliang\project001> python -m uiautomator2 init
[I 221022 09:30:55 init:156] uiautomator2 version: 2.16.19
[I 221022 09:30:55 init:373] Install minicap, minitouch
[D 221022 09:30:55 init:59] Use cached assets: C:\Users\Administrator.DESKTOP-QBI8U5R\.uiautomator2\cache\minitouch-f7a806902f\minitouch
[D 221022 09:30:55 init:238] Push to /data/local/tmp/minitouch:0755
[I 221022 09:30:55 init:379] Android R (sdk:30) has no minicap resource
[D 221022 09:30:56 init:256] apk-debug package-info: None
[D 221022 09:30:56 init:257] apk-debug-test package-info: None
[I 221022 09:30:56 init:388] Install com.github.uiautomator, com.github.uiautomator.test 2.3.3
[D 221022 09:30:56 init:167] Shell: ('pm', 'uninstall', 'com.github.uiautomator')
[D 221022 09:30:56 init:167] Shell: ('pm', 'uninstall', 'com.github.uiautomator.test')
[D 221022 09:30:56 init:59] Use cached assets: C:\Users\Administrator.DESKTOP-QBI8U5R\.uiautomator2\cache\app-uiautomator.apk-d3f17174fb\app-uiautomator.apk
[D 221022 09:30:56 init:238] Push to /data/local/tmp/app-uiautomator.apk:0644
[D 221022 09:30:57 init:167] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator.apk')
[I 221022 09:31:16 init:331] - app-uiautomator.apk installed
[D 221022 09:31:16 init:59] Use cached assets: C:\Users\Administrator.DESKTOP-QBI8U5R\.uiautomator2\cache\app-uiautomator-test.apk-652bf9e13c\app-uiautomator-test.apk
[D 221022 09:31:16 init:238] Push to /data/local/tmp/app-uiautomator-test.apk:0644
[D 221022 09:31:16 init:167] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator-test.apk')
[I 221022 09:31:23 init:331] - app-uiautomator-test.apk installed
[D 221022 09:31:23 init:167] Shell: ('/data/local/tmp/atx-agent', 'server', '--stop')
[I 221022 09:31:23 init:346] Install atx-agent 0.10.0
[D 221022 09:31:23 init:59] Use cached assets: C:\Users\Administrator.DESKTOP-QBI8U5R\.uiautomator2\cache\atx-agent_0.10.0_linux_armv7.tar.gz-cf3333ae7b\atx-agent_0.10.0_linux_armv7.tar.gz
[D 221022 09:31:23 init:238] Push to /data/local/tmp/atx-agent:0755
[D 221022 09:31:24 init:167] Shell: ('/data/local/tmp/atx-agent', 'server', '--nouia', '-d', '--addr', '127.0.0.1:7912')
[I 221022 09:31:24 init:350] Check atx-agent version
[D 221022 09:31:24 init:359] Forward: local:tcp:15957 -> remote:tcp:7912
[D 221022 09:31:25 init:359] Forward: local:tcp:15957 -> remote:tcp:7912
[D 221022 09:31:25 init:362] atx-agent version 0.10.0
[D 221022 09:31:25 init:366] device wlan ip: 192.168.88.105
Successfully init AdbDevice(serial=c******0)
(myenv) PS D:\xiamingliang\project001>
image-20221022093240021.png

并且寨蹋,在你的手機上,也會有一個ATX

image-20221022120642058.png
image-20221022120700547.png

所以剛才我們輸入的uiautomator init 命令會執(zhí)行哪些操作呢 扔茅?

他會安裝3個包已旧,

  • uiautomator-server:谷歌原生的uiautomator

  • atx-agent:uiautomator的守護進程

  • atx-agent增加遠程控制的功能,依賴minicap和minitouch這兩個工具

初始化完成后召娜,既可以在這個目錄下C:\Users\Administrator\.uiautomator2\cache看到這幾個包了:(這個不重要运褪,看不到也沒啥影響的)

image-20221022094008376.png

五、連接手機

1.通過USB方式

>>>> import uiautomator2
>>> conn = uiautomator2.connect_usb('c******0')   #adb devices顯示的設備id
>>> print(conn.info)
[D 221022 09:42:32 __init__:656] [pid:10108] [c******0] kill process(ps): uiautomator
[D 221022 09:42:33 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 40.0s
[D 221022 09:42:34 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 39.0s
[D 221022 09:42:35 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 37.9s
[D 221022 09:42:36 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 36.9s
[D 221022 09:42:37 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 35.9s
[D 221022 09:42:38 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 34.9s
[D 221022 09:42:39 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 33.8s
[D 221022 09:42:40 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 32.8s
[D 221022 09:42:41 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 31.8s
[D 221022 09:42:42 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 30.8s
[D 221022 09:42:42 __init__:691] [pid:10108] [c******0] show float window
[D 221022 09:42:43 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 29.6s
[I 221022 09:42:43 __init__:638] [pid:10108] [c******0] uiautomator back to normal
{'currentPackageName': 'com.miui.home', 'displayHeight': 2276, 'displayRotation': 0, 'displaySizeDpX': 393, 'displaySizeDpY': 873, 'displayWidth': 1080, 'productName': 'munch', 'screenOn': True, 'sdkInt': 31, 'naturalOrientation': True}
>>>

可以看到設備的信息可以這個正常獲取并顯示玖瘸。

2.通過WIFI方式

>>>> import uiautomator2
>>> conn = uiautomator2.connect_adb_wifi('your phone ip address')
>>> print(conn.info)

3.uiautomator2其他常用的function

function:
 _fix_wifi_addr:
 _is_tmq_production:
 connect: Args:
 connect_adb_wifi: Run adb connect, and then call connect_usb(..)
 connect_usb: Args:
 connect_wifi: Args:
 deprecated: This is a decorator which can be used to mark functions
 list2cmdline:
 namedtuple: Returns a new subclass of tuple with named fields.
 process_safe_wrapper: threadsafe for process calls
 retry: Returns a retry decorator.
 setup_logger: Configures and returns a fully configured logger instance, no hassles.
 thread_safe_wrapper:

六:用python做個調(diào)試程序

import uiautomator2
import time
import random
def douyin():    
 #通過usb連接;c******0是adb devices -l列出的被調(diào)試手機id(手機上要點擊允許)
 conn=uiautomator2.connect_usb('c******0')     #替換成自己的
 # 打開抖音
 conn(text="抖音").click()          #沒打開時使用秸讹,打開程序后就不需要了
 runcount_init = 0
 runcount_all = 10   #劃幾次
 while runcount_init < runcount_all:
 # 滑動視頻
 # 向下滑
conn.swipe(round(random.uniform(700,720),2),round(random.uniform(1650,1670),2),round(random.uniform(830,850),2),round(random.uniform(300,320),2),duration=random.uniform(0.01,0.1))      #調(diào)整duration控制滑動速度
 time.sleep(random.uniform(0.5,1.5))
 # 向上滑
conn.swipe(round(random.uniform(810,800),2),round(random.uniform(1160,1200),2),round(random.uniform(775,785),2),round(random.uniform(1750,1800),2),duration=random.uniform(0.01,0.1))
 time.sleep(random.uniform(0.5,1.5))
 runcount_init = runcount_init + 1

if __name__=='__main__':    
 douyin()

七:補充信息

幾種常用的定位APP方式如下:

1.text:a(text=“抖音”).click()   #需要程序在屏幕最上層(如果APP在抽屜或者不在當前頁面則無法定位并打開的)
2.resourceid:a(resourceId=“com.smartisanos.clock:id/text_stopwatch”).click()
3.classname:a(className=“android.widget.TextView”).click()
4.description :a(description="…").click()</pre>

更多的方法參考:

名稱 描述
text text是指定文本的元素
textContains text中包含有指定文本的元素
textMatches text符合指定正則的元素
textStartsWith text以指定文本開頭的元素
className className是指定類名的元素
classNameMatches className類名符合指定正則的元素
description description是指定文本的元素
descriptionContains description中包含有指定文本的元素
descriptionMatches description符合指定正則的元素
descriptionStartsWith description以指定文本開頭的元素
checkable 可檢查的元素,參數(shù)為True雅倒,F(xiàn)alse
checked 已選中的元素璃诀,通常用于復選框,參數(shù)為True蔑匣,F(xiàn)alse
clickable 可點擊的元素劣欢,參數(shù)為True棕诵,F(xiàn)alse
longClickable 可長按的元素,參數(shù)為True凿将,F(xiàn)alse
scrollable 可滾動的元素校套,參數(shù)為True,F(xiàn)alse
enabled 已激活的元素牧抵,參數(shù)為True笛匙,F(xiàn)alse
focusable 可聚焦的元素,參數(shù)為True犀变,F(xiàn)alse
focused 獲得了焦點的元素妹孙,參數(shù)為True,F(xiàn)alse
selected 當前選中的元素获枝,參數(shù)為True涕蜂,F(xiàn)alse
packageName packageName為指定包名的元素
packageNameMatches packageName為符合正則的元素
resourceId resourceId為指定內(nèi)容的元素
resourceIdMatches resourceId為符合指定正則的元素

對于元素的定位還支持:

1.子元素定位

2.兄弟定位

3.相對定位

4.XPATH定位等

八、視頻效果

點擊這里查看視頻效果映琳。

視頻鏈接地址:
https://xml-test01.oss-cn-shanghai.aliyuncs.com/%E6%88%91%E7%9A%84%E5%8D%9A%E5%AE%A2%E8%B5%84%E6%96%99/20221022_110858.mp4

本文作者:夏明亮

轉(zhuǎn)載請標注出處: http://www.reibang.com/u/b637fb6e14fe

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末机隙,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子萨西,更是在濱河造成了極大的恐慌有鹿,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件谎脯,死亡現(xiàn)場離奇詭異葱跋,居然都是意外死亡,警方通過查閱死者的電腦和手機源梭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進店門娱俺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人废麻,你說我怎么就攤上這事荠卷。” “怎么了烛愧?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵油宜,是天一觀的道長。 經(jīng)常有香客問我怜姿,道長慎冤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任沧卢,我火速辦了婚禮蚁堤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘但狭。我一直安慰自己披诗,他們只是感情好撬即,可當我...
    茶點故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著藤巢,像睡著了一般搞莺。 火紅的嫁衣襯著肌膚如雪息罗。 梳的紋絲不亂的頭發(fā)上掂咒,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機與錄音迈喉,去河邊找鬼绍刮。 笑死,一個胖子當著我的面吹牛挨摸,可吹牛的內(nèi)容都是我干的孩革。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼得运,長吁一口氣:“原來是場噩夢啊……” “哼膝蜈!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起熔掺,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤饱搏,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后置逻,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體推沸,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年券坞,在試婚紗的時候發(fā)現(xiàn)自己被綠了鬓催。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡恨锚,死狀恐怖宇驾,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情猴伶,我是刑警寧澤飞苇,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站蜗顽,受9級特大地震影響布卡,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜雇盖,卻給世界環(huán)境...
    茶點故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一忿等、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧崔挖,春花似錦贸街、人聲如沸庵寞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽捐川。三九已至,卻和暖如春逸尖,著一層夾襖步出監(jiān)牢的瞬間古沥,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工娇跟, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留岩齿,地道東北人。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓苞俘,卻偏偏與公主長得像盹沈,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子吃谣,可洞房花燭夜當晚...
    茶點故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容