python伪代码之使用appium自动化加微信好友运行代码【内向即垃圾--王奕君】

这个代码只能玩玩,属于失败的,可以做到自动化加好友,但是加到一定的好友,就会被封号。

对于微信是如何识别机器行为,不确定。但即使是人工去加好友,到一定的数量也是会被封号的。

然而微信号又是实名制的,一旦被封号,再次登录需要手机验证码等一系列的安全验证。

所以网上那种可以无限制加微信好友的软件,我只能说一定有这样的高手,并且曾经接触过高级爬虫开发的工作,让我

明白任何想法都是可以通过程序实现的,只是你是否达到这个境界而已。

# -*- coding:utf-8 -*-
import os
import time

from appium import webdriver
from selenium.common.exceptions import WebDriverException, TimeoutException
from selenium.webdriver.support.ui import WebDriverWait

from demain_error import AppiumResponseTimeoutException
from time_tool import timeout

PHONES = ['13816853491',
          '13501613595',
          '13641988990',
          '15800685954',
          '15801966697',
          '13761539185',
          '15821927262',
          '13611928589',
          '13818295148',
          '15801805715',
          '13761020478',
          '13818558470',
          '15921333801',
          '13917742558',
          '15921989862',
          '13918206876',
          '13918852943',
          '15821622858',
          '13916578091',
          '13661796133',
          '13916745605',
          '15901768433',
          '13524126070',
          '13651967273',
          '13801764366',
          '13818983613',
          '13818278350',
          '13917397307',
          '15900977447',
          '13621847657',
          '13661648762',
          '13636672795',
          '13816396991',
          '15921440179',
          '13916923588',
          '13916338493']

SUCCESS = 0


def add_friend():
    '''添加朋友'''
    WebDriverWait(wd, 15, 0.5).until(
        lambda wd:
        wd.find_element_by_id("com.tencent.mm:id/g_")).click()
    WebDriverWait(wd, 15, 0.5).until(
        lambda wd: wd.find_element_by_xpath('//android.widget.TextView[contains(@text, "添加朋友")]')).click()
    WebDriverWait(wd, 15, 0.5).until(lambda wd: wd.find_element_by_xpath(
        '//android.widget.TextView[contains(@text, "微信号/QQ号/手机号")]')).click()
    res = search_phone()
    print(res)


def search_phone():
    '''搜索电话号'''
    print('正在搜索电话号')
    friend_id = 0
    for phone in PHONES:
        friend_id += 1
        WebDriverWait(wd, 5, 0.5).until(lambda wd: wd.find_element_by_id("com.tencent.mm:id/ht")).send_keys(
            u'{}'.format(phone))  # 搜索栏输入内容
        WebDriverWait(wd, 5, 0.5).until(
            lambda wd:
            wd.find_element_by_id("com.tencent.mm:id/kn")).click()
        search_phone_res()
        all_phone = PHONES.index(phone) + 1
        file = all_phone - int(SUCCESS)
        res=('总共添加好友数:{}'.format(all_phone), '添加好友成功数:{}'.format(SUCCESS), '因为各种原因添加好友失败数:{}'.format(file))
        print(res)
    return res


def search_phone_res():
    '''搜索电话结果'''
    print('正在搜索电话结果')
    try:
        WebDriverWait(wd, 5, 0.5).until_not(
            lambda wd: wd.find_element_by_android_uiautomator('new UiSelector().text("发消息")'), message='已经是好友')
        WebDriverWait(wd, 5, 0.5).until(
            lambda wd: wd.find_element_by_android_uiautomator('new UiSelector().text("添加到通讯录")'),
            message='被搜帐号状态异常,无法显示|该用户不存在').click()
        try:
            WebDriverWait(wd, 5, 0.5).until_not(
                lambda wd: wd.find_element_by_android_uiautomator('new UiSelector().text("添加到通讯录")'),
                message='操作过于频繁|对方被添加好友过于频繁')
        except TimeoutException as e:
            raise WebDriverException(msg=e)
    except WebDriverException as e:
        print('返回错误信息:{}'.format(e))
        wd.keyevent(4)
        WebDriverWait(wd, 5, 0.5).until(
            lambda wd: wd.find_element_by_id("com.tencent.mm:id/ht")).clear()
    else:
        send_verification()


def send_verification():
    '''验证申请'''
    print('正在验证申请')
    try:
        WebDriverWait(wd, 5, 0.5).until(
            lambda wd: wd.find_element_by_android_uiautomator('new UiSelector().text("发送")'),
            message='对方已经把你加入黑名单').click()
        try:
            WebDriverWait(wd, 5, 0.5).until_not(
                lambda wd: wd.find_element_by_android_uiautomator('new UiSelector().text("发送")'),
                message='对方被添加好友过于频繁|你回复太快了,休息一下')
            global SUCCESS
            SUCCESS += 1
        except TimeoutException as e:
            raise WebDriverException(msg=e)
    except WebDriverException as e:
        print('返回错误信息:{}'.format(e))
        wd.keyevent(4)
        wd.keyevent(4)
    else:
        wd.keyevent(4)
    finally:
        WebDriverWait(wd, 5, 0.5).until(
            lambda wd: wd.find_element_by_id("com.tencent.mm:id/ht")).clear()


def back_home():
    '''返回微信主页'''
    print('返回微信主页')
    while True:
        try:
            wd.find_element_by_android_uiautomator('new UiSelector().text("发现")')
            break
        except Exception as e:
            wd.keyevent(4)


def people_nearby():
    '''查找附近的人'''
    print('正在查找附近的人')
    WebDriverWait(wd, 5, 0.5).until(
        lambda wd: wd.find_element_by_android_uiautomator('new UiSelector().text("发现")')).click()
    WebDriverWait(wd, 10, 0.5).until(
        lambda wd: wd.find_element_by_android_uiautomator('new UiSelector().text("附近的人")')).click()
    wd.keyevent(82)
    time.sleep(6)
    wd.keyevent(82)
    WebDriverWait(wd, 10, 0.5).until(lambda wd: wd.find_element_by_android_uiautomator('new UiSelector().text("附近的人")'))
    get_people_nearby()


def get_people_nearby():
    '''获取附近的好友'''
    print('正在获取附近的朋友')
    nearby_success_count = 0
    nearby_fail_count = 0
    while True:
        WebDriverWait(wd, 10, 0.5).until(lambda wd: wd.find_element_by_xpath(
            "//android.widget.FrameLayout[1]//android.widget.ListView/android.widget.LinearLayout[contains(@index,1)]")).click()
        if add_people_nearby():
            nearby_success_count += 1
        else:
            nearby_fail_count += 1
        back_nearby = -1
        while back_nearby < 0:
            try:
                WebDriverWait(wd, 5, 0.5).until(
                    lambda wd: wd.find_element_by_android_uiautomator('new UiSelector().text("附近的人")'))
                back_nearby += 1
            except WebDriverException as e:
                wd.keyevent(4)
        swipeUp(1000)
        print('当前请求附近好友成功个数:{}'.format(nearby_success_count), '当前请求附近好友失败个数:{}'.format(nearby_fail_count))
        # return nearby_count


@timeout(20)  # 超时设置
def add_people_nearby():
    '''加附近的好友'''
    nearby_people = 0
    print('正在添加附近的好友')
    try:
        WebDriverWait(wd, 5, 0.5).until(
            lambda wd: wd.find_element_by_android_uiautomator('new UiSelector().text("打招呼")'), message='已经是好友').click()
        WebDriverWait(wd, 5, 0.5).until(lambda wd: wd.find_element_by_id("com.tencent.mm:id/d0c")).send_keys(
            u'你好,我是微信的产品经理,想和你聊聊')
        wd.find_element_by_android_uiautomator('new UiSelector().text("发送")').click()
    except (WebDriverException, AppiumResponseTimeoutException(err='响应超时25秒')) as e:
        print(e)
    else:
        try:
            WebDriverWait(wd, 5, 0.5).until(
                lambda wd: wd.find_element_by_android_uiautomator('new UiSelector().text("打招呼")'))
            nearby_people += 1
        except WebDriverException as e:
            print(e)
    finally:
        return nearby_people


def swipeUp(t):  # t是毫秒,越小滑动速度越快
    '''滑动的范围'''
    width = wd.get_window_size()['width']
    wd.swipe(width / 2, 390, width / 2, 186.6, t)


def import_phonenumber():
    y = os.system('adb -s emulator-5554 shell pm clear com.android.providers.contacts')
    # y=os.system(r'adb pull /data/data/com.android.providers.contacts/databases/contacts.db E:\bakup')
    print(y)


def linkman_add():
    pass


def quit():
    '''结束程序'''
    wd.close_app()
    wd.quit()


def ctrl_shift():
    '''切换输入法'''
    os.system("adb shell ime set com.android.inputmethod.pinyin/.PinyinIME")


'''打开微信'''
desired_caps = {'platformName': 'Android',
                'deviceName': '8d74acbf',
                'platformVersion': '7.1.1',
                'appPackage': 'com.tencent.mm',
                'appActivity': 'com.tencent.mm.ui.LauncherUI',
                'appWaitActivity': 'com.tencent.mm.ui.LauncherUI',
                'unicodeKeyboard': 'True',
                'resetKeyboard': 'True',
                'newCommandTimeout': '60'}  # 240s 没有指令返回home键
# 'autoAcceptAlerts':'True'}
# 'launchMode':'singleTask'}
# 'newCommandTimeout':1800}   #默认等待表签显示时间
wd = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# people_nearby()
add_friend()
# import_phonenumber()
 

猜你喜欢

转载自blog.csdn.net/qq_37995231/article/details/81193694