閱讀文本大概需要 12 分鐘。
目 標 場 景
以今日頭條極速版為首谜诫,包含趣頭條攻旦、東方頭條、全名小視頻在內(nèi)的 App 都有看新聞且预、視頻送金幣的活動,當金幣達到一定量后遍尺,就可以提現(xiàn)到微信涮拗、支付包。
如果單純靠人工去點擊看新聞和視頻鼓择,會浪費很多時間就漾。本文的目標是利用 Python 驅動手機去看新聞和視頻,每天幫我們薅一個早餐錢摆出。
下面以「東方頭條」客戶端為例展開說明首妖。
準 備 工 作
元素的定位需要借助 Airtes,需要在 PC 端進行安裝骑丸,具體可以參考上一篇妒貌。另外這里以 Android 系統(tǒng)手機為例,所以提前配置好了 adb 環(huán)境菊碟。
另外在刺,需要在虛擬環(huán)境內(nèi)安裝「pocoui」庫。
pip3 install pocoui
分 析 思 路
首先我們需要利用 adb 命令打開東方頭條 App魄幕。
使用 Android Studio 的 Analyze Apk 工具颖杏,可以獲取應用包名和初始 Activity 分別是:
com.songheng.eastnews
com.oa.eastfirst.activity.WelcomeActivity
然后使用「adb shell am start」命令去打開東方頭條的客戶端。
# 應用包名
package_name = 'com.songheng.eastnews'
# 初始Activity
activity = 'com.oa.eastfirst.activity.WelcomeActivity'
def start_my_app(package_name, activity_name):
"""
打開應用
adb shell am start -n com.tencent.mm/.ui.LauncherUI
:param package_name:
:return:
"""
os.popen('adb shell am start -n %s/%s' % (package_name, activity_name))
start_my_app(package_name, activity)
由于第一次打開應用咙轩,會有一個顯示廣告的界面阴颖,我們需要通過 Airtest 獲取到「跳過廣告」元素,執(zhí)行點擊操作钾菊,讓應用快速進入到主頁面侠畔。
def __pre_and_skip_ads(self):
"""
預加載和跳過廣告
:return:
"""
# 1.廣告頁面元素的出現(xiàn)
# 兩種樣式:跳過、跳過廣告*秒
try:
poco('com.songheng.eastnews:id/aoy').wait_for_appearance(10)
except Exception as e:
print('等待廣告元素異常')
print(e)
ads_element = poco(name='com.songheng.eastnews:id/aoy', textMatches='^跳過廣告.*$')
ads_element1 = poco(name='android.widget.TextView', text='跳過')
# 跳過廣告(0s)
if ads_element.exists():
print('跳過廣告1!!!')
ads_element.click()
if ads_element1.exists():
print('跳過廣告2!!!')
ads_element1.click()
# 2.等到到達主頁面
poco('com.songheng.eastnews:id/g_').wait_for_appearance(120)
到達主頁面之后尤勋,我們發(fā)現(xiàn)主要有 3 種方式獲取金幣,分別是「閱讀文章」最冰、「播放視頻」、「播放小視頻」赌朋,另外一種獲取金幣的方式就是歸納于其他方式中篇裁。
首先达布,我們使用 Airtest 來分析新聞 Tab 的列表。
新聞列表可以通過獲取 name 為「com.songheng.eastnews:id/g_」 的元素黍聂,再取其所有子元素就能獲取到第一頁的新聞列表产还。
lv_elements = poco('com.songheng.eastnews:id/g_').children()
if not lv_elements.exists():
print('新聞列表不存在')
return
# 遍歷每一條新聞
for news_element in lv_elements:
# 新聞標題
news_title = news_element.offspring('com.songheng.eastnews:id/pb')
#作者
author_element = news_element.offspring('com.songheng.eastnews:id/a4f')
需要注意的是,上面獲取的新聞列表中有很多廣告和點擊下載的內(nèi)容愈诚,需要過濾掉。
# 4.過濾廣告
# 到這里標識此條新聞:是一條有效的新聞【包含廣告】
# 注意:部分廣告【包含點擊標題就自動下載尤溜,左下角顯示廣告字眼等】要過濾掉
# 場景一:
if news_element.attr('name') == 'android.widget.FrameLayout':
print('廣告汗唱!這是一個FrameLayout廣告哩罪,標題是:%s' % news_title.get_text())
continue
# 常見二:點擊標題直接下載其他應用
ads_tips_element = news_element.offspring(name='com.songheng.eastnews:id/a4f', text='廣告通')
if ads_tips_element.exists():
print('廣告!這是一個【廣點通】廣告碘耳,標題是:%s' % news_title.get_text())
continue
# 常見三:有效角標識是廣告的圖標【奇虎廣告】
ads_tips_element2 = news_element.offspring('com.songheng.eastnews:id/q5')
if ads_tips_element2.exists():
print('廣告框弛!廣告標題是:%s' % news_title.get_text())
continue
只有判斷是一條正常的新聞,才點擊新聞的標題元素進入新聞詳情頁面斗搞,如果右下角的「時間條元素」存在才代表閱讀此篇新聞能獲取到金幣慷妙。
red_coin_element = poco('com.songheng.eastnews:id/aq8')
if not red_coin_element.exists():
print('當前新聞沒有紅包,返回虑啤!')
self.__back_keyevent()
continue
為了更真實的模擬人為看新聞這一操作狞山,隨機地模擬向上或向下滑動屏幕绩蜻。
這里設置每篇文章閱讀時間為 30 秒,閱讀完成之后办绝,執(zhí)行返回操作孕蝉,直到回到主界面,這樣就完成了查看一篇新聞獲取金幣的流程降淮。
oldtime = datetime.datetime.now()
while True:
self.__swipe(True if random.randint(0, 1) == 0 else False)
newtime = datetime.datetime.now()
interval_time = (newtime - oldtime).seconds
if interval_time >= 30:
print('閱讀30秒新聞完成')
break
self.__read_key_news()
接著可以從下往上滑動頁面,獲取到新的頁面的新聞列表媒惕,循環(huán)的進行閱讀来庭。
while True:
self.watch_news_recommend()
print('查看一頁完成,繼續(xù)查看下一頁的新聞肴盏。')
# 滑動下一頁的新聞
poco.swipe([0.5, 0.8], [0.5, 0.3], duration=1)
另外帽衙,注意應用的標題欄隔一段時間可以領取金幣,定義一個方法去領取恍飘。
def get_top_title_coin(self):
"""
頂部金幣領取
僅僅在新聞首頁的時候才可以領取
:return:
"""
get_coin_element = poco(name='com.songheng.eastnews:id/arq', text="領取")
if get_coin_element.exists():
print('頂部有金幣可以領惹吹妗!')
get_coin_element.click()
print('領完金幣后可以關閉對話框!')
# 關掉對話框
self.__back_keyevent()
else:
print('頂部沒有金幣或者不在首頁')
然后可以點擊視頻 Tab 去切換到視頻頁面肢专。和看新聞一樣焦辅,這里同樣是獲取視頻列表元素去遍歷查看視頻。
def __video(self):
"""
查看視頻
:return:
"""
poco('com.songheng.eastnews:id/ko').click()
while True:
# 視頻列表
poco('com.songheng.eastnews:id/a0z').wait_for_appearance()
sleep(2)
self.__read_key_news()
video_elements = poco('com.songheng.eastnews:id/a0z').children()
print('video items是否存在:')
print(video_elements.exists())
# 遍歷視頻
# 注意:視頻播放完全可以提前返回
for video_element in video_elements:
# 1.標題元素
video_title_element = video_element.offspring('com.songheng.eastnews:id/a3q')
# 播放按鈕
video_play_element = video_element.offspring('com.songheng.eastnews:id/nj')
# 2.必須保證【視頻標題】和【播放按鈕】都可見
if not video_title_element.exists() or not video_play_element.exists():
continue
# 3.標題
video_title = video_element.offspring('com.songheng.eastnews:id/a3q').get_text()
print('當前視頻的標題是:%s,播放當前視頻' % video_title)
# 點擊播放視頻
video_play_element.focus("center").click()
# 4.播放視頻
self.play_video()
print('播放下一個視頻')
self.__back_keyevent()
# 滑動到下一頁的視頻
poco.swipe([0.5, 0.8], [0.5, 0.3], duration=0.2)
觀看小視頻獲取金幣的操作最為簡單。首先切換到小視頻 Tab狈醉,獲取到第一個視頻的元素惠险,執(zhí)行點擊操作,開始播放小視頻渣慕。
poco('com.songheng.eastnews:id/kr').click()
# 加載出列表元素,點擊第一項進入
poco('com.songheng.eastnews:id/a0p').child('com.songheng.eastnews:id/g_').wait_for_appearance(60)
poco('com.songheng.eastnews:id/a0p').child('com.songheng.eastnews:id/g_').children()[0].click()
最后只需要等待視頻播放 30 秒之后逊桦,使用 swipe 函數(shù)向左滑動屏幕切換到下一個視頻,就可以實現(xiàn)反復播放獲取金幣的操作强经。
while True:
sleep(30)
# 向左滑動
poco.swipe([0.9, 0.5], [0.1, 0.5], duration=0.2)
結 果 結 論
執(zhí)行程序夕凝,手機會自動打開東方頭條客戶端,執(zhí)行閱讀新聞逮矛、看視頻和小視頻的一系列操作转砖。
最后只需要將閱讀新聞、播放視頻和小視頻的時間分配好晋控,一個客戶端獲取金幣達到上限后姓赤,就關閉應用,然后切換到其他 App 客戶端蝌焚,繼續(xù)閱讀新聞和視頻誓斥,就可以實現(xiàn)薅多個平臺的羊毛。
本文首發(fā)于公眾號「 AirPython 」毕谴,后臺回復「 東方頭條 」即可獲取完整代碼距芬。