用 Python + Appium 的方式自动化清理微信僵尸好友

用 Python + Appium 的方式自动化清理微信僵尸好友

文 | 某某白米饭

来源:Python 技术「ID: pythonall」

用 Python + Appium 的方式自动化清理微信僵尸好友

随着微信的使用时间越长,微信好友也越来越多,有些好友将你删除了你也不知道。当我们发消息的时候会出现下面扎心的一幕,然后默默将他删除

用 Python + Appium 的方式自动化清理微信僵尸好友

使用 Appium

基础的 appium 使用在公众号文章 《解放双手,提高生产力,这款神器你值得拥有》 中已经讲过了,这里使用最新 1.20.0 版本的 appium,旧版本会出现真机微信闪退的情况

安装一下 Python 用到的模块

pip install Appium-Python-Client

获取好友列表

在 Pycharm 中配置一下启动环境

desired_capabilities = {
    'platformName': 'Android', # 操作系统
    'deviceName': '2a254a02', # 设备 ID,使用 cmd 中 adb devices 命令得到
    'platformVersion': '10.0.10', # 设备版本号,在手机设置中查看
    'appPackage': 'com.tencent.mm', # app 包名
    'appActivity': 'com.tencent.mm.ui.LauncherUI', # app 启动时主 Activity
    'noReset': True # 是否保留 session 信息 避免重新登录
}

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_capabilities)
print('微信启动')

下图是 appium 启动后截图

用 Python + Appium 的方式自动化清理微信僵尸好友

点击红框中按钮,将上面的参数填上,点击 start Session

启动后点击刷新按钮,看到的界面和真机上一样了,在真机上点击通讯录按钮并刷新界面

用 Python + Appium 的方式自动化清理微信僵尸好友用 Python + Appium 的方式自动化清理微信僵尸好友
在 appium 界面点击一个好友,可以看到这个好友有一个 content-desc 和 resource-id 代表了昵称和资源 id

用 Python + Appium 的方式自动化清理微信僵尸好友

然后我们用 Python 获取所有的好友昵称


# 所有好友
friends = []
def get_friends():
    # 好友id
    address_list = driver.find_elements_by_id('com.tencent.mm:id/dy5')
    for address in address_list:
        # 昵称
        friend = address.get_attribute('content-desc')
        # 过滤掉自己、微信团队、文件夹传输助手
        if friend != '某某白米饭' and friend != '微信团队' and friend != '文件夹传输助手':
            friends.append(friend)
        # 获取到最后一个好友返回
        if friend == '

猜你喜欢

转载自blog.51cto.com/15127519/2684013
今日推荐