macaca之zfb

import base64
import random
import traceback
from io import BytesIO

import re
from PIL import Image
from macaca import WebDriver, WebElement
import time

from util.log import logger

desired_caps = {
    'platformName': 'Android',  # iOS, Android.
    'deviceName': '69T7N1*********',
    'platformVersion': '5.1.1',
    'reuse': '3',  # 0: 启动并安装 app。1 (默认): 卸载并重装 app。 2: 仅重装 app。3: 在测试结束后保持 app 状态。
    'package': 'com.eg.android.AlipayGphone',  # zhifubao
    # 'activity':'com.tencent.mm.ui.LauncherUI'
}

phones = ['phone1', 'phone2', 'phone3']


class ZhiFuBao(object):
    def __init__(self):

        self.server_url = {
            'hostname': '127.0.0.1',
            'port': 3456
        }
        #输入法相应按键对应的坐标
        self.keyboard = {'1': (300, 1200), '2': (550, 1200), '3': (800, 1200), '4': (300, 1350), '5': (550, 1350),
                         '6': (800, 1350), '7': (300, 1500), '8': (550, 1500), '9': (800, 1500), '0': (550, 1700),
                         'enter': (1000, 1700), 'finish': (1000, 1350)}

        self.time_sleep = 2

        self.driver = WebDriver(desired_caps, self.server_url)
        self.driver.init()

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type:
            print(traceback.format_exc())
        print('over')
    #头像处理,并以base64编码保存
    def handle_img(self, driver=None):
        img = Image.open(BytesIO(base64.b64decode(driver.take_screenshot())))
        img2 = img.crop((370, 275, 710, 615))
        img2.save('icon.png')
        with open('icon.png', 'rb') as f:
            content = f.read()
            base64encode = base64.b64encode(content)
        return base64encode

    def fun(self):
        time.sleep(self.time_sleep)
        # 点击右上角符号'+'
        self.driver.touch('tap', {'x': 1000, 'y': 150})
        time.sleep(self.time_sleep)
        # 点击'添加朋友'
        self.driver.touch('tap', {'x': 900, 'y': 400})
        time.sleep(self.time_sleep)

        # 点击‘支付宝账户/手机号/淘宝会员名’
        self.driver.touch('tap', {'x': 173, 'y': 280})

        for phone in phones:
            time.sleep(self.time_sleep)
            # 输入手机号
            self.driver.touch('tap', {'x': self.keyboard['enter'][0], 'y': self.keyboard['enter'][1]})          
            for num in phone:
                x, y = self.keyboard[num]
                self.driver.touch('tap', {'x': x, 'y': y})
                time.sleep(random.random() / 10)
            # 输入完成,点击完成
            self.driver.touch('tap', {'x': self.keyboard['finish'][0], 'y': self.keyboard['finish'][1]})
            time.sleep(self.time_sleep)
            # 如果账户不存在
            if re.search('(账户|账号)不存在', self.driver.source):
                print(phone, '账号不存在')
                # 点击确定
                self.driver.touch('tap', {'x': 800, 'y': 1050})
                time.sleep(self.time_sleep)
                # 点击右上方符号'X',清除号码
                self.driver.wait_for_element('xpath',
                                             '//*[@resource-id="com.alipay.mobile.ui:id/social_btn_normal_clear"]',
                                             timeout=60).touch('tap')
                continue

            # 点击显示更多
            self.driver.wait_for_element('xpath',
                                         '//*[@resource-id="com.alipay.android.phone.wallet.profileapp:id/tv_show_more"]').touch(
                'tap')
            time.sleep(self.time_sleep)
            # 头像获取
            icon = self.handle_img(self.driver)
            # 获取名字
            name = self.driver.element('xpath',
                                       '//*[@resource-id="com.alipay.android.phone.wallet.profileapp:id/tv_name"]').text
            # 获取左侧文本
            left = self.driver.elements_by_id('com.alipay.android.phone.wallet.profileapp:id/tv_left')
            left_text = [item.text for item in left]
            # 获取右侧文本
            right_text = list()
            for item in self.driver.elements_by_id('com.alipay.android.phone.wallet.profileapp:id/tv_right'):
                content_desc = item.get_property('content-desc')
                # 是否已实名
                if 'description' in content_desc:
                    right_text.append(content_desc['description'])
                else:
                    right_text.append(content_desc['text'])

            result = dict(zip(left_text, right_text))
            result['name'] = name
            result['icon'] = icon
            print(result)

            # 回退
            self.driver.wait_for_element('xpath', '//*[@resource-id="com.alipay.mobile.ui:id/title_bar_back_button"]',
                                         timeout=60).touch('tap')
            time.sleep(self.time_sleep)
            # 点击右上方符号'X',清除号码
            self.driver.wait_for_element('xpath', '//*[@resource-id="com.alipay.mobile.ui:id/social_btn_normal_clear"]',
                                         timeout=60).touch('tap')



def crawl():
    with ZhiFuBao() as zfb:
        zfb.fun()


if __name__ == '__main__':
    crawl()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值