閱讀文本大概需要 12 分鐘。
目 標(biāo) 場 景
以今日頭條極速版為首,包含趣頭條、東方頭條、全名小視頻在內(nèi)的 App 都有看新聞、視頻送金幣的活動(dòng),當(dāng)金幣達(dá)到一定量后,就可以提現(xiàn)到微信、支付包。
如果單純靠人工去點(diǎn)擊看新聞和視頻,會浪費(fèi)很多時(shí)間。本文的目標(biāo)是利用 Python 驅(qū)動(dòng)手機(jī)去看新聞和視頻,每天幫我們薅一個(gè)早餐錢。
下面以「東方頭條」客戶端為例展開說明。
準(zhǔn) 備 工 作
元素的定位需要借助 Airtes,需要在 PC 端進(jìn)行安裝,具體可以參考上一篇。另外這里以 Android 系統(tǒng)手機(jī)為例,所以提前配置好了 adb 環(huán)境。
另外,需要在虛擬環(huán)境內(nèi)安裝「pocoui」庫。
pip3 install pocoui
分 析 思 路
首先我們需要利用 adb 命令打開東方頭條 App。
使用 Android Studio 的 Analyze Apk 工具,可以獲取應(yīng)用包名和初始 Activity 分別是:
com.songheng.eastnews
com.oa.eastfirst.activity.WelcomeActivity
然后使用「adb shell am start」命令去打開東方頭條的客戶端。
# 應(yīng)用包名
package_name = 'com.songheng.eastnews'
# 初始Activity
activity = 'com.oa.eastfirst.activity.WelcomeActivity'
def start_my_app(package_name, activity_name):
"""
打開應(yīng)用
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)
由于第一次打開應(yīng)用,會有一個(gè)顯示廣告的界面,我們需要通過 Airtest 獲取到「跳過廣告」元素,執(zhí)行點(diǎn)擊操作,讓應(yīng)用快速進(jìn)入到主頁面。
def __pre_and_skip_ads(self):
"""
預(yù)加載和跳過廣告
: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.等到到達(dá)主頁面
poco('com.songheng.eastnews:id/g_').wait_for_appearance(120)
到達(dá)主頁面之后,我們發(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:
# 新聞標(biāo)題
news_title = news_element.offspring('com.songheng.eastnews:id/pb')
#作者
author_element = news_element.offspring('com.songheng.eastnews:id/a4f')
需要注意的是,上面獲取的新聞列表中有很多廣告和點(diǎn)擊下載的內(nèi)容,需要過濾掉。
# 4.過濾廣告
# 到這里標(biāo)識此條新聞:是一條有效的新聞【包含廣告】
# 注意:部分廣告【包含點(diǎn)擊標(biāo)題就自動(dòng)下載,左下角顯示廣告字眼等】要過濾掉
# 場景一:
if news_element.attr('name') == 'android.widget.FrameLayout':
print('廣告!這是一個(gè)FrameLayout廣告,標(biāo)題是:%s' % news_title.get_text())
continue
# 常見二:點(diǎn)擊標(biāo)題直接下載其他應(yīng)用
ads_tips_element = news_element.offspring(name='com.songheng.eastnews:id/a4f', text='廣告通')
if ads_tips_element.exists():
print('廣告!這是一個(gè)【廣點(diǎn)通】廣告,標(biāo)題是:%s' % news_title.get_text())
continue
# 常見三:有效角標(biāo)識是廣告的圖標(biāo)【奇虎廣告】
ads_tips_element2 = news_element.offspring('com.songheng.eastnews:id/q5')
if ads_tips_element2.exists():
print('廣告!廣告標(biāo)題是:%s' % news_title.get_text())
continue
只有判斷是一條正常的新聞,才點(diǎn)擊新聞的標(biāo)題元素進(jìn)入新聞詳情頁面,如果右下角的「時(shí)間條元素」存在才代表閱讀此篇新聞能獲取到金幣。
red_coin_element = poco('com.songheng.eastnews:id/aq8')
if not red_coin_element.exists():
print('當(dāng)前新聞沒有紅包,返回!')
self.__back_keyevent()
continue
為了更真實(shí)的模擬人為看新聞這一操作,隨機(jī)地模擬向上或向下滑動(dòng)屏幕。
這里設(shè)置每篇文章閱讀時(shí)間為 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()
接著可以從下往上滑動(dòng)頁面,獲取到新的頁面的新聞列表,循環(huán)的進(jìn)行閱讀。
while True:
self.watch_news_recommend()
print('查看一頁完成,繼續(xù)查看下一頁的新聞。')
# 滑動(dòng)下一頁的新聞
poco.swipe([0.5, 0.8], [0.5, 0.3], duration=1)
另外,注意應(yīng)用的標(biāo)題欄隔一段時(shí)間可以領(lǐng)取金幣,定義一個(gè)方法去領(lǐng)取。
def get_top_title_coin(self):
"""
頂部金幣領(lǐng)取
僅僅在新聞首頁的時(shí)候才可以領(lǐng)取
:return:
"""
get_coin_element = poco(name='com.songheng.eastnews:id/arq', text="領(lǐng)取")
if get_coin_element.exists():
print('頂部有金幣可以領(lǐng)取!')
get_coin_element.click()
print('領(lǐng)完金幣后可以關(guān)閉對話框!')
# 關(guān)掉對話框
self.__back_keyevent()
else:
print('頂部沒有金幣或者不在首頁')
然后可以點(diǎn)擊視頻 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.標(biāo)題元素
video_title_element = video_element.offspring('com.songheng.eastnews:id/a3q')
# 播放按鈕
video_play_element = video_element.offspring('com.songheng.eastnews:id/nj')
# 2.必須保證【視頻標(biāo)題】和【播放按鈕】都可見
if not video_title_element.exists() or not video_play_element.exists():
continue
# 3.標(biāo)題
video_title = video_element.offspring('com.songheng.eastnews:id/a3q').get_text()
print('當(dāng)前視頻的標(biāo)題是:%s,播放當(dāng)前視頻' % video_title)
# 點(diǎn)擊播放視頻
video_play_element.focus("center").click()
# 4.播放視頻
self.play_video()
print('播放下一個(gè)視頻')
self.__back_keyevent()
# 滑動(dòng)到下一頁的視頻
poco.swipe([0.5, 0.8], [0.5, 0.3], duration=0.2)
觀看小視頻獲取金幣的操作最為簡單。首先切換到小視頻 Tab,獲取到第一個(gè)視頻的元素,執(zhí)行點(diǎn)擊操作,開始播放小視頻。
poco('com.songheng.eastnews:id/kr').click()
# 加載出列表元素,點(diǎn)擊第一項(xiàng)進(jìn)入
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ù)向左滑動(dòng)屏幕切換到下一個(gè)視頻,就可以實(shí)現(xiàn)反復(fù)播放獲取金幣的操作。
while True:
sleep(30)
# 向左滑動(dòng)
poco.swipe([0.9, 0.5], [0.1, 0.5], duration=0.2)
結(jié) 果 結(jié) 論
執(zhí)行程序,手機(jī)會自動(dòng)打開東方頭條客戶端,執(zhí)行閱讀新聞、看視頻和小視頻的一系列操作。
最后只需要將閱讀新聞、播放視頻和小視頻的時(shí)間分配好,一個(gè)客戶端獲取金幣達(dá)到上限后,就關(guān)閉應(yīng)用,然后切換到其他 App 客戶端,繼續(xù)閱讀新聞和視頻,就可以實(shí)現(xiàn)薅多個(gè)平臺的羊毛。
本文首發(fā)于公眾號「 AirPython 」,后臺回復(fù)「 東方頭條 」即可獲取完整代碼。