移动端爬虫--单设备自动化(二)

文集 移动端爬虫

单设备自动化

  • 准备

    假设软件环境都已配置

    一台android虚拟机, 我的是夜神端口是62025

  • 启动

    1. 连接设备

      [图片上传失败...(image-cd54b7-1532398731012)]

    2. 运行appium,默认启动4723端口

    9549518-67c27f4a26217afb.png
    image

    [图片上传失败...(image-72a668-1532398731012)]

    备注:

    ​ 1) 设备提前装好app, appActivity和appPackage的值下面介绍

    ​ 2)deviceName: 如果是真机,填设备的型号(如我的华为畅享7: SLA-LA00)

    1. 审查元素

      [图片上传失败...(image-cc0a5d-1532398731012)]

      上图已经标记出了appPackage,至于appActivity指的是android app的入口activity组件,默认是MainActivity,可以通过反编译工(AndroidKiller)具查看。

    2. capability设置

      {
        "platformName": "Android",
        "automationName": "Appium",
        "platformVersion": "5.1.1",
        "appPackage": "com.ss.android.ugc.aweme",
        "appActivity": "com.ss.android.ugc.aweme.main.MainActivity",
        "noReset": "True",
        "deviceName": "Emulator"
      }
      

到此appium操控app成功。下面给出一段代码,使用python操控

# -*- coding: utf-8 -*-
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver.common.touch_action import TouchAction


# the emulator is sometimes slow and needs time to think
SLEEPY_TIME = 1

desired_caps = {
  "platformName": "Android",
  "automationName": "Appium",
  "platformVersion": "5.1.1",
  "appPackage": "com.ss.android.ugc.aweme",
  "appActivity": "com.ss.android.ugc.aweme.main.MainActivity",
  "noReset": "True",
  "deviceName": "Emulator"
}
class App(object):

    def __init__(self):
        self.driver = webdriver.Remote('http://%s:%d/wd/hub' % ('127.0.0.1', 4723), desired_caps)
        self.wait = WebDriverWait(self.driver, 30)
        # {'width':w, 'height':h}
        self.window = self.driver.get_window_size()

    def set_up(self):
        id = self.driver.session_id
        print(id)

    def tear_down(self):
        self.driver.quit()

    def wait_element_clickable(self, locator):
        return self.wait.until(
            EC.element_to_be_clickable(locator)
        )

    def back(self):
        self.driver.back()

    def center(self, left_top, right_bottom):
        x = (left_top[0] + right_bottom[0]) / 2
        y = (left_top[1] + right_bottom[1]) / 2
        return x, y

    def click_by_xy(self, left_top, right_bottom):
        location = self.center(left_top, right_bottom)
        self.driver.tap([location], 200)

    def swip(self, x1, y1, x2, y2, duration=None):
        action = TouchAction(self.driver)
        action.press(x=x1, y=y1) \
            .wait(300) \
            .move_to(x=x2, y=y2) \
            .release() \
            .perform()

    def app_is_installed(self, app_package):
        self.driver.is_app_installed(app_package)

    @property
    def page_source(self):
        return self.driver.page_source

    def quit(self):
        self.driver.quit()


if '__main__' == __name__:
    t = App()

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值