薅羊毛 | 讓Python天天幫你薅一個早餐錢



閱讀文本大概須要 12 分鐘。php


1css

目 標 場 景python


以今日頭條極速版爲首,包含趣頭條、東方頭條、全民小視頻在內的 App 都有看新聞、視頻送金幣的活動,當金幣達到必定量後,就能夠提現到微信、支付寶。android


若是單純靠人工去點擊看新聞和視頻,會浪費不少時間。本文的目標是利用 Python 驅動手機去看新聞和視頻,天天幫咱們薅一個早餐錢。spring


下面以「東方頭條客戶端爲例展開說明。shell


2bash

準 備 工 做微信


元素的定位須要藉助 Airtes,須要在 PC 端進行安裝,具體能夠參考上一篇。另外這裏以 Android 系統手機爲例,因此提早配置好了 adb 環境。app


另外,須要在虛擬環境內安裝pocoui庫。dom


pip3 install pocoui


3

分 析 思 路


首先咱們須要利用 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 獲取到跳過廣告」元素,執行點擊操做,讓應用快速進入到主頁面。


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(01) == 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.50.8], [0.50.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.50.8], [0.50.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.90.5], [0.10.5], duration=0.2)



4

結 果 結 論


執行程序,手機會自動打開東方頭條客戶端,執行閱讀新聞、看視頻和小視頻的一系列操做。


最後只須要將閱讀新聞、播放視頻和小視頻的時間分配好,一個客戶端獲取金幣達到上限後,就關閉應用,而後切換到其餘 App 客戶端,繼續閱讀新聞和視頻,就能夠實現薅多個平臺的羊毛。



我已經將所有源碼上傳到後臺上,公衆號回覆「 東方頭條 」便可得到。

若是你以爲文章還不錯,請你們點贊分享下。你的確定是我最大的鼓勵和支持。


本文分享自微信公衆號 - IT爛筆頭(nj_android)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索