以今日頭條極速版爲首,包含趣頭條、東方頭條、全名小視頻在內的 App 都有看新聞、視頻送金幣的活動,當金幣達到必定量後,就能夠提現到微信、支付包。android
若是單純靠人工去點擊看新聞和視頻,會浪費不少時間。本文的目標是利用 Python 驅動手機去看新聞和視頻,天天幫咱們薅一個早餐錢。spring
下面以「東方頭條」客戶端爲例展開說明。shell
元素的定位須要藉助 Airtes,須要在 PC 端進行安裝,具體能夠參考上一篇。另外這裏以 Android 系統手機爲例,因此提早配置好了 adb 環境。bash
另外,須要在虛擬環境內安裝「pocoui」庫。微信
pip3 install pocoui複製代碼
首先咱們須要利用 adb 命令打開東方頭條 App。app
使用 Android Studio 的 Analyze Apk 工具,能夠獲取應用包名和初始 Activity 分別是:dom
com.songheng.eastnewside
com.oa.eastfirst.activity.WelcomeActivity函數
而後使用「adb shell am start」命令去打開東方頭條的客戶端。工具
# 應用包名package_name = 'com.songheng.eastnews'# 初始Activityactivity = '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 獲取到「跳過廣告」元素,執行點擊操做,讓應用快速進入到主頁面。
def __pre_and_skip_ads(self): """ 預加載和跳過廣告 :return: """ # 1.廣告頁面元素的出現 # 兩種樣式:跳過、跳過廣告*秒 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)複製代碼
到達主頁面以後,咱們發現主要有 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')複製代碼
須要注意的是,上面獲取的新聞列表中有不少廣告和點擊下載的內容,須要過濾掉。
# 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 秒,閱讀完成以後,執行返回操做,直到回到主界面,這樣就完成了查看一篇新聞獲取金幣的流程。
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()複製代碼
接着能夠從下往上滑動頁面,獲取到新的頁面的新聞列表,循環的進行閱讀。
```
while True:
self.watch_news_recommend()
print('查看一頁完成,繼續查看下一頁的新聞。')
# 滑動下一頁的新聞
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,獲取到第一個視頻的元素,執行點擊操做,開始播放小視頻。
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 函數向左滑動屏幕切換到下一個視頻,就能夠實現反覆播放獲取金幣的操做。
while True: sleep(30) # 向左滑動 poco.swipe([0.9, 0.5], [0.1, 0.5], duration=0.2)複製代碼
執行程序,手機會自動打開東方頭條客戶端,執行閱讀新聞、看視頻和小視頻的一系列操做。
最後只須要將閱讀新聞、播放視頻和小視頻的時間分配好,一個客戶端獲取金幣達到上限後,就關閉應用,而後切換到其餘 App 客戶端,繼續閱讀新聞和視頻,就能夠實現薅多個平臺的羊毛。
我本文首發於公衆號「 AirPython 」,後臺回覆「 東方頭條 」便可獲取完整代碼。