python爬虫之Appium手机APP爬虫
一、Appium工作原理(详情见:https://www.cnblogs.com/sophia194910/p/7515165.html)

Appium的功能其实很简单:监听一个端口,然后接收由client发送来的command,翻译这些command,把这些command转成移动设备可以理解的形式发送给移动设备,然后移动设备执行完这些command后把执行结果返回给appium server,appium server再把执行结果返回给client。
在这里client其实就是发起command的设备,一般来说就是我们代码执行的机器,执行appium测试代码的机器。狭义点理解,可以把client理解成是代码,这些代码可以是java/ruby/python/js的,只要它实现了webdriver标准协议就可以。
二、Appium环境搭建
1、需要用到的软件包如下:
(1)node.js
(2)Java SDK
(3)Android SDK
(4)Appium Windows
(5)夜神模拟器
(6)Python
(7)Appium-python-Client
2、node.js(参考:https://blog.csdn.net/u010654583/article/details/85125377)
(1)下载地址:http://nodejs.cn/download/
(2)安装好后,执行npm install -g appium-doctor(npm是node.js的包管理工具,下载appium-doctor可以检测Appium的环境搭建是否成功)
(3)执行appium-doctor检测Appium的环境搭建情况

上图打对勾的地方即是要坚持的环境变量配置情况,只有全部打对勾,才说明环境搭配成功
3、Java SDK
(1)下载地址:https://www.oracle.com/technetwork/java/javase/downloads/index.html
(2)安装Java SDK文件,并配置环境变量,最后检测是否安装成功(参考:https://jingyan.baidu.com/article/d45ad148ba5ab169552b80d3.html)
4、Android SDK
(1)下载地址:https://developer.android.com/studio/(现在已经没有单独的解压包了,只有跟Android Studio一起下载)
(2)安装好后,配置环境变量,最后检测是否安装成功(参考:https://www.cnblogs.com/nebie/p/9145627.html)
5、Appium Windows
(1)下载地址:http://appium.io/
(2)安装Appium Windows
6、夜神模拟器
(1)下载地址:https://www.yeshen.com/
(2)安装
7、Python与Appium-Python-Client的安装就不讲了
8、使用appium-doctor检测环境是否安装成功,同第2步
9、测试是否能正常启动
根据appium的工作原理:
(1)测试client与appium是否能通信成功


当运行代码时,Appium就会响应,虽然是404,但只是因为Appium没有找到执行命令的设备
(2)测试appium与模拟器是否能通信成功



不管遇见什么样的错误,只要把Origin error:后面的报错信息复制到百度上找寻解决办法基本都能找到,这里表示不能找到连接了的安卓设备,所以我们先要连接安装设备,
在命令行中:
检测是否有连接的设备,下面表示没有连接的设备

连接已经打开的安卓设备,连接成功

再检测一下,发现有了

接下来就可以点击Start session,将Appium连接上安卓设备,连接成功与设备同步


现在我们使用代码驱动Appium,Appium再将命令转给安卓设备,模拟器打开,说明成功了,可以正式开始抓取APP的数据了

现在可以解释一下platformName、platformVersion、deviceName、appPackage、appActivity、noReset这几个参数的来历了
platformName(系统名称):Android、IOS、Windows、Linux
plaformVersion(系统版本)
deviceName(设备名称,即设备运行的地址,默认为127.0.0.1:62001)
appPackage(要打开哪个软件)、appActivity(要打开的软件的活动页面)
appPackage、appActivity两个参数可以通过点开模拟器上想要打开的软件,然后通过命令行获取

10、用法基本与selenium类似,下附简单的代码(定位时,能使用id,尽量不使用xpath)
#!/usr/bin/env python
# coding: utf-8
# In[63]:
from appium import webdriver
# In[64]:
from time import sleep
# In[65]:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
# In[66]:
from selenium.webdriver.common.by import By
# In[67]:
desired_caps = {
"platformName": "Android",
"platformVersion": "5.1.1",
"deviceName": "127.0.0.1:62001",
"appPackage": "com.wuba",
"appActivity": ".activity.launch.LaunchActivity",
"noReset": "False" # 清除缓存
}
# In[68]:
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
# In[69]:
# 获取手机屏幕长宽
screenWidth = driver.get_window_size()["width"]
screenHeight = driver.get_window_size()["height"]
# In[70]:
# beijing_xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.support.v4.view.ViewPager/android.widget.RelativeLayout/android.support.v7.widget.RecyclerView/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.view.View/android.widget.LinearLayout[1]"
# WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, beijing_xpath))).click()
# In[72]:
hotCityId = "com.wuba:id/fl_hot"
hotCity = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "com.wuba:id/fl_hot")))
# In[73]:
hotCity.find_element_by_xpath("//android.widget.LinearLayout[1]").click()
# In[74]:
# driver.find_element_by_id("com.wuba:id/launch_op_bt_ok").click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "com.wuba:id/launch_op_bt_ok"))).click()
# In[75]:
ershoufang_xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.View/android.view.View/android.widget.LinearLayout/android.widget.LinearLayout[1]/android.widget.GridView/android.view.View[4]/android.widget.ImageView"
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, ershoufang_xpath))).click()
# In[76]:
ershoufang_xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.support.v4.view.ViewPager/android.widget.FrameLayout/android.widget.RelativeLayout[1]/android.widget.FrameLayout/android.widget.RelativeLayout/android.support.v7.widget.RecyclerView/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.LinearLayout/android.view.View[1]/android.widget.LinearLayout[1]/android.widget.TextView"
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, ershoufang_xpath))).click()
# In[78]:
items_id = "com.wuba:id/recyclerView"
items = driver.find_element_by_id(items_id)
while 1:
driver.swipe(screenWidth*0.5, screenHeight*0.85, screenWidth*0.5, screenHeight*0.25, 500)
ls = items.find_elements_by_xpath("//android.widget.LinearLayout")
sleep(3)
# In[ ]: