1、背景简介
在抓取APP的数据时,在网上不经意间搜到有关Airtest的内容,就突发奇想也想用它来实现一下。由于之前在对APP抓包时,fiddler、charles统统失败了,抓取不到https的包,且手机就会连不上网,实在有点力不从心。也了解过有关appium自动化测试,好吧,环境实在难配置的一批,恶心死我了,但是Airtest就比较友好,基本都集成在一起了,况且直接与python对接,实在不要太方便。闲话少说,在进行之前,先下载好相关软件,网上教程都可以搜到的,这里就不再赘述,这不是重点。在这里要注意的是:python里需要安装airtest、pocoui。emmm,前提条件,抓取的数据要比较少,不然挺耗时的,其次,有可能遇到验证码拼图,恶心,我遇到了,可能爬取频率太高,被捕捉到了,不过,你对图片处理如果比较好,也是挺容易解决的,计算耦合点,以及滑动的距离即可,再次,只能抓到屏幕上存在的所有数据,其余不可以。
2.代码实现
# -*- encoding=utf8 -*-
__author__ = "Mr 雷"
from airtest.core.api import *
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
import csv
import numpy as np
auto_setup(__file__)
f = open('上海二手房信息.csv','a+',newline='')
writer = csv.writer(f)
writer.writerow(['描述','单价','朝向','装修','年代','类型','房本年限','电梯','唯一住房','楼层','总价','户型','面积','预算','小区','商圈'])
# 远程设别连接(模拟器)
connect_device('android:///emulator-5554?cap_method=javacap&touch_method=adb')
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
xy = poco.get_screen_size()
x = xy[0]
y = xy[1]
describes = []
# 写入文件
def to_csv(data):
writer.writerow([data.get('描述',''),data.get('单价',''),data.get('朝向',''),
data.get('装修',''),data.get('年代',''),data.get('类型',''),
data.get('楼层',''),data.get('总价',''),data.get('户',''),data.get('厅',''),data.get('卫',''),
data.get('面积', ''),data.get('预算',''),data.get('小区',''),
data.get('商圈',''),data.get('轨交','')])
# 获取内容
def get_info():
father_node = poco(name='com.anjuke.android.app:id/home_page_child_recycler_view').wait(10)
child_nodes = father_node.child('android.widget.LinearLayout').wait(10)
# 遍历详情节点,进入详情页
for node in child_nodes:
infos = {}
try:
node.child('android.widget.RelativeLayout').click()
except:
continue
# 等待页面缓冲
sleep(np.random.random() + np.random.randint(4, 6))
# 如果没有定位到元素,舍弃部分,返回上一个页面
try:
describe = poco('com.anjuke.android.app:id/bigTitleTv').get_text()
# 如果前面采集过这部分信息,不会在重复采集,结束当前循环
if describe in describes:
keyevent('KEYCODE_BACK')
sleep(np.random.random() + np.random.randint(4, 6))
continue
total_price = poco('com.anjuke.android.app:id/bigPriceDescTv').get_text()
room_num = poco('com.anjuke.android.app:id/bigHouseTypeRoomNumTv').get_text()
hall_num = poco('com.anjuke.android.app:id/bigHouseTypeHallNumTv').get_text()
toile_num = poco('com.anjuke.android.app:id/bigHouseTypeToiletNumTv').get_text()
area = poco('com.anjuke.android.app:id/bigAreaDescTv').get_text()
infos['总价'] = total_price
infos['室'] = room_num
infos['厅'] = hall_num
infos['卫'] = toile_num
infos['面积'] = area
infos['描述'] = describe
describes.append(describe)
# 下部分页面显示
swipe((0.5*x,0.75*y),(0.5*x,0.5*y),duration=np.random.random()+np.random.randint(1,3))
infos['单价'] = poco('com.anjuke.android.app:id/priceTv').get_text().split(' ')[1]
infos['楼层'] = poco('com.anjuke.android.app:id/floorTv').get_text().split(' ')[1]
infos['朝向'] = poco('com.anjuke.android.app:id/orientationTv').get_text().split(' ')[1]
infos['类型'] = poco('com.anjuke.android.app:id/typeTv').get_text().split(' ')[1]
infos['装修'] = poco('com.anjuke.android.app:id/decorationTv').get_text().split(' ')[1]
infos['年代'] = poco('com.anjuke.android.app:id/ageTv').get_text().split(' ')[1]
infos['预算'] = poco('com.anjuke.android.app:id/budget_content_text_view').get_text()
infos['小区'] = poco('com.anjuke.android.app:id/comm_name_tv').get_text()
infos['商圈'] = poco('com.anjuke.android.app:id/comm_address_tv').get_text()
infos['轨交'] = poco('com.anjuke.android.app:id/second_house_metro_text_view').get_text()
except Exception as e:
print(e)
if len(infos):
to_csv(infos)
keyevent('KEYCODE_BACK')
sleep(np.random.random() + np.random.randint(4, 6))
# 滑动屏幕
def swipe_screen():
sleep(1)
swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=np.random.random()+np.random.randint(3,5))
for i in range(0,5000):
print(count)
get_info()
swipe_screen()
f.close()
3. 结果

这是我们市区的二手房价格,我感觉也已经很高了,hhhhh。
4. 结语
感觉好玩就小试牛刀,当然了,这个主要是做脚本的,如果感兴趣的话,可以深入的,做个游戏脚本刷金币、抢红包啥的,都是行的通的。至此就结束了,有问题可以留言交流。
网友评论