使用 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 启动后截图
点击红框中按钮,将上面的参数填上,点击 start Session
启动后点击刷新按钮,看到的界面和真机上一样了,在真机上点击通讯录按钮并刷新界面
在 appium 界面点击一个好友,可以看到这个好友有一个 content-desc 和 resource-id 代表了昵称和资源 id
然后我们用 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 == '