ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python-app自动化查找微信僵尸好友

2021-03-30 23:01:27  阅读:205  来源: 互联网

标签:name python 微信 app driver 好友 text self friend


通过微信转账的方式来判断,这里没有做删除操作,可以自己去写,对于已经僵尸好友和转账异常的用户会存储到对应文件中,方便查看自己手动删除或重新添加

转账页面不同的提示如下几种,所以要分类型操作:

         

不说太多,上代码

from appium import webdriver
from setting_app import desired_caps
import time
import os


class Set_File:
    '''
    创建保存已经验证过的用户文件,非好友文件,异常好友文件
    '''
    def __init__(self,dirname):
        self.file_names = ['已验证.txt', '非好友.txt', '转账异常.txt']
        self.save_path = dirname
        if not os.path.exists(self.save_path):
            os.makedirs(self.save_path)

    def set_files(self):
        for file in self.file_names:
            filepath = self.save_path + '/' + file
            with open(filepath, 'a+', encoding='utf-8') as f:  # 以写入的方式创建文件
                pass

    def get_friendlist(self):
        '''
        :return: 获取已经验证过的好友列表
        '''
        name_list = []
        filepath = self.save_path + '/' + self.file_names[0]
        with open(filepath,'r+',encoding='utf-8') as f:  # 读取文件中的好友替换换行符后添加到列表中
            name = f.readlines()
            for i in name:
                name_list.append(i.replace('\n',''))
        not_verified = list(set(name_list))
        return [not_verified,filepath]

    def get_notfriendlist(self):
        '''
        :return: 获取非好友列表
        '''
        name_list = []
        filepath = self.save_path + '/' + self.file_names[1]
        with open(filepath, 'r+', encoding='utf-8') as f:  # 读取文件中的好友替换换行符后添加到列表中
            name = f.readlines()
            for i in name:
                name_list.append(i.replace('\n',''))
        not_friend = list(set(name_list))
        return [not_friend,filepath]

    def get_abnormityuser(self):
        '''
        :return:转账不是提示非好友,提示其他异常的用户
        '''
        name_list = []
        filepath = self.save_path + '/' + self.file_names[2]
        with open(filepath, 'r+', encoding='utf-8') as f:  # 读取文件中的好友替换换行符后添加到列表中
            name = f.readlines()
            for i in name:
                hint1 = '对方微信号已被限制登录,为保障你的资金安全,暂时无法完成交易。'
                hint2 = '请先完善交易信息,核实对方身份后,谨慎判断是否继续支付。'
                name_list.append(i.replace('\n', '').replace(hint1,'').replace(hint2,''))
        abnormity_friend = list(set(name_list))
        return [abnormity_friend,filepath]


class Wxfriend_Verify:

    def __init__(self):
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)  # 连接Appium Server,初始化自动化环境
        self.driver.implicitly_wait(5)  # 设置缺省等待时间
        self.driver.find_elements_by_id('dtx')[3].click()  # 进入当前用户页面
        myname = self.driver.find_element_by_id('fz_').text  # 获取用户当前用户昵称,需要将当前用户添加到不要验证的好友中
        not_verifylist = ['微信团队', '文件传输助手',myname]
        files = Set_File(myname)
        files.set_files()   # 创建文件
        self.not_verify = files.get_friendlist()[0] + not_verifylist    # 不需要验证或已经验证的用户
        self.not_friends = files.get_notfriendlist()[0]  # 用来添加非好友对象的列表对象
        self.abnormity_friend = files.get_abnormityuser()[0]   # 用来添加转账异常但提示不是非好友列表对象
        self.verifidpath = files.get_friendlist()[1]  # 已验证好友文件路径
        self.not_friendpath = files.get_notfriendlist()[1]   # 非好友文件路径
        self.abnormitypath = files.get_abnormityuser()[1]  # 异常好友文件路径
        self.address_list = self.driver.find_elements_by_id('dtx')[1].click()  # 点击进入通讯录页面
        # 以下是确定需要滑动的坐标
        text = 'new UiSelector().text("新的朋友")'
        size = self.driver.get_window_size()
        self.start_x = size['width'] * 0.5  # 起始滑动的横坐标为整个页面的中间,结束位置也可以使用
        friend_list = self.driver.find_elements_by_id('ft6')  # 获取当前页面通讯录好友目录列表
        self.start_y = friend_list[-1].location['y']  # 开始的滑动的纵坐标选择最后一个通讯录页面最后一个对象的纵坐标
        self.end_y = self.driver.find_element_by_android_uiautomator(text).location['y']  # 结束滑动的纵坐标选择通讯录页面的最新好友的这个对象的纵坐标

    def verify_friend(self):   # 判断验证好友类型
        friend_list = self.driver.find_elements_by_id('ft6')  # 获取当前页面通讯录好友目录列表
        for goodfriend in friend_list:
            '''
            通过循环对获取到的每一个好友表中的人员通过转账时的提示信息来验证是否是被删除或拉黑
            '''
            friend_name = goodfriend.text  # 获取好友昵称,方便之后如过不是好友的时候添加到非好友列表中 和 如果是本人或者微信团队时通过判断跳过验证
            if friend_name in self.not_verify:  # 如果是不用验证的好友就跳出当前循环
                continue
            else:
                goodfriend.click()  # 点击好友对象进入好友菜单页面
                text = 'new UiSelector().text("发消息")'
                self.driver.find_element_by_android_uiautomator(text).click()  # 点击好友菜单页面中的“发消息”进入消息页面
                self.driver.find_element_by_id('au0').click()  # 点击消息界面的加号按钮弹出菜单
                self.driver.find_elements_by_id('rr')[5].click()  # 点击转账按钮
                self.driver.find_element_by_id('jf4').send_keys('0.01')  # 输入转账金额0.01
                self.driver.find_element_by_id('e6c').click()  # 点击转账
                self.not_verify.append(friend_name)  # 将已经验证过的好友添加到不再验证的列表中
                try:
                    #  转账正常好友
                    text = 'new UiSelector().text("请输入支付密码")'
                    self.driver.find_element_by_android_uiautomator(text)
                    with open(self.verifidpath, 'a+', encoding='utf-8') as f:  # 将已经验证过的好友添加到好友文件中
                        f.write(friend_name + '\n')
                    self.driver.find_element_by_xpath('.//android.view.ViewGroup[@content-desc="关闭"]').click()  # 关闭转账密码输入
                except Exception:
                    # 转账异常好友
                    try:
                        # 非好友及异常好友
                        text = 'new UiSelector().text("知道了")'
                        hint = '你不是收款方好友,对方添加你为好友后才能发起转账'
                        hint_now = self.driver.find_element_by_id('ffh').text
                        if hint_now == hint:   # 如果提示是非好友,添加到非好友中
                            # 非好友对象操作
                            self.not_friends.append(friend_name)  # 将非好友的昵称添加到非好友列表中
                            with open(self.verifidpath, 'a+', encoding='utf-8') as f:  # 将已经验证过的好友添加到好友文件中
                                f.write(friend_name + '\n')
                            with open(self.not_friendpath, 'a+', encoding='utf-8') as f:  # 将不是好友的对象添加到文件中
                                f.write(friend_name + '\n')
                        else:
                            # 未提示非好友的其他类型提示的对象操作
                            self.abnormity_friend.append(friend_name)  # 将非好友的昵称添加到转账异常列表中
                            with open(self.verifidpath, 'a+', encoding='utf-8') as f:  # 将已经验证过的好友添加到好友文件中
                                f.write(friend_name + '\n')
                            with open(self.abnormitypath, 'a+', encoding='utf-8') as f:  # 将转账异常对象添加到文件中
                                f.write(friend_name + hint_now + '\n')
                        self.driver.find_element_by_android_uiautomator(text).click()  # 关闭非好友和异常提示
                    except Exception:
                        # 交易提醒
                        text = 'new UiSelector().text("交易提醒")'
                        self.driver.find_element_by_android_uiautomator(text)
                        with open(self.verifidpath, 'a+', encoding='utf-8') as f:  # 将已经验证过的好友添加到好友文件中
                            f.write(friend_name + '\n')
                        with open(self.abnormitypath,'a+',encoding='utf-8') as f:  # 将转账异常对象添加到文件中
                            f.write(friend_name + '请先完善交易信息,核实对方身份后,谨慎判断是否继续支付。' + '\n')
                        text = 'new UiSelector().text("取消支付")'
                        self.driver.find_element_by_android_uiautomator(text).click()
                self.driver.find_element_by_id('ei').click()  # 退出到消息页面
                self.driver.find_element_by_id('uo').click()  # 退出到“微信”页面
                self.driver.find_elements_by_id('dtx')[1].click()  # 进入通讯录页面

    def swipe_pag(self):
        i = 1
        while i < 1000:
            '''
            判断是否已经滑动到底部来决定是否执行滑动
            '''
            try:
                text = 'new UiSelector().textContains("位联系人")'
                self.driver.find_element_by_android_uiautomator(text)
                self.verify_friend()
                sum_verify = len(self.not_verify) - 3
                print('已为您验证完成,共验证  %d  个好友,以下用户不再是您的好友或已经被拉黑及转账异常,请及时处理'%sum_verify)
                for not_friend in self.not_friends:
                    print('%s : 已不是你的好友或已将你拉黑'%not_friend)
                for abnormity_friend in self.abnormity_friend:
                    print('%s : 转账存在异常,请检查'%abnormity_friend)
                self.driver.quit()
                return '已全部验证'
            except Exception:
                # 滑动时间选择长一点,要是太短滑动会超过设定的滑动距离
                self.verify_friend()  # 验证完当前页面的好友后进行滑动操作
                self.driver.swipe(start_x=self.start_x, start_y=self.start_y, end_x=self.start_x,
                                  end_y=self.end_y, duration=1500)
                i += 1


def run_script():
    '''
    由于手机加载反应延迟可能会定位不到元素报错,所以加一个循环,当报错时重新执行,直到执行到最后一个好友时跳出循环
    '''

    start_time = time.time()
    for _ in range(100):
        wx = Wxfriend_Verify()
        try:
            wx.swipe_pag()
            break
        except Exception:
            wx.driver.quit()
    end_time = time.time()
    print('用时 %s 分钟' % round((end_time - start_time)/60, 2))


if __name__ == '__main__':
    run_script()

 

标签:name,python,微信,app,driver,好友,text,self,friend
来源: https://blog.csdn.net/JBY2020/article/details/115337948

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有