刷金幣全自動腳本 | 讓Python每天幫你薅一個早餐錢(送源碼)


刷金幣全自動腳本 | 讓Python每天幫你薅一個早餐錢(送源碼)

測試開發社區  6天前
文章轉載自公眾號  AirPython , 作者 星安果

 

 

閱讀文本大概需要 12 分鍾。

 

1

目 標 場 景

 

 

以今日頭條極速版為首,包含趣頭條、東方頭條、全名小視頻在內的 App 都有看新聞、視頻送金幣的活動,當金幣達到一定量后,就可以提現到微信、支付包。

 

如果單純靠人工去點擊看新聞和視頻,會浪費很多時間。本文的目標是利用 Python 驅動手機去看新聞和視頻,每天幫我們薅一個早餐錢。

 

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

 

2

准 備 工 作

 

元素的定位需要借助 Airtes,需要在 PC 端進行安裝,具體可以參考上一篇。另外這里以 Android 系統手機為例,所以提前配置好了 adb 環境。

 

另外,需要在虛擬環境內安裝「pocoui」庫。

 

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(0, 1) == 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)

 

 

4

結 果 結 論

 

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

 

最后只需要將閱讀新聞、播放視頻和小視頻的時間分配好,一個客戶端獲取金幣達到上限后,就關閉應用,然后切換到其他 App 客戶端,繼續閱讀新聞和視頻,就可以實現薅多個平台的羊毛。

 

如果你覺得文章還不錯,請大家點贊分享下。你的肯定是我最大的鼓勵和支持。

 

我已經將看新聞刷金幣全自動化腳本全部源碼上傳到github上:https://github.com/xingag/app_spider/tree/master/%E4%B8%9C%E6%96%B9%E5%A4%B4%E6%9D%A1          


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM