前言
wx视频号关注越来越多,手动取消又太费劲,最近正好在看了点AirTest,于是选择用它。
代码
注意
- 由下向上执行,所以要先滑动到底部,并且保留200个
- 由于屏幕分辨率问题,坐标相关参数需自行修改
# -*- encoding=utf8 -*-
__author__ = "x.j"
import re
import time
from airtest.core.api import *
auto_setup(__file__)
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
start_time = time.time()
totalCount = 0
unSubCount = 0
# 滑动起始坐标
swipeXy = (700,1900)
# 取消关注点击坐标
unSubTouch = (797,1967)
# 关注的视频号(xxxx)
text = poco("android:id/text1").get_text()
# 使用正则表达式提取数字
match = re.search(r'\((\d+)\)', text)
if match:
# 获取关注数量
totalCount = int(match.group(1))
unSubCount = totalCount - 200
if totalCount == 0:
print("我的关注数量为 " + str(count))
else:
# 向下滑动,从最后一个开始取消
for i in range(100000):
swipe(swipeXy, vector=[0.0266, -0.5125],steps=5,duration=0.1)
# 检测是否到达列表底部
if poco(text=f"关注的{totalCount}人").exists():
print('!!!!!!!!!到底啦!!!!!!!!!')
break
for i in range(unSubCount):
# 长按最后一个,因为屏幕分辨率差异,这里坐标自己找自己的
touch(v=unSubTouch,duration=1)
poco(text="取消关注").click()
sleep(0.1)
else:
print("没有找到数字")
end_time = time.time()
elapsed_time = end_time - start_time
print(f"关注总数:{totalCount}\n本次任务执行:{unSubCount}\n耗时:{elapsed_time:.6f}秒")
总结
- 思路有很多,哪个方便顺手就选哪个,AirTest就在手边于是选用它完成
- 除了直接操作手机,还可以去操作微信PC端。