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',
'deviceName': '69T7N1*********',
'platformVersion': '5.1.1',
'reuse': '3',
'package': 'com.eg.android.AlipayGphone',
}
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')
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)
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)
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()