薅羊毛 | 让Python天天帮你薅一个早餐钱


一、目 标 场 景


以今日头条极速版为首,包含趣头条、东方头条、全名小视频在内的 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 」,后台回复「 东方头条 」便可获取完整代码。

相关文章
相关标签/搜索