2020-03-28Airtest爬虫——以安居客APP为例

2020-03-28Airtest爬虫——以安居客APP为例

2023年6月29日发(作者:)

2020-03-28Airtest爬⾍——以安居客APP为例1、背景简介在抓取APP的数据时,在⽹上不经意间搜到有关Airtest的内容,就突发奇想也想⽤它来实现⼀下。由于之前在对APP抓包时,fiddler、charles统统失败了,抓取不到https的包,且⼿机就会连不上⽹,实在有点⼒不从⼼。也了解过有关appium⾃动化测试,好吧,环境实在难配置的⼀批,恶⼼死我了,但是Airtest就⽐较友好,基本都集成在⼀起了,况且直接与python对接,实在不要太⽅便。闲话少说,在进⾏之前,先下载好相关软件,⽹上教程都可以搜到的,这⾥就不再赘述,这不是重点。在这⾥要注意的是:python⾥需要安装airtest、pocoui。emmm,前提条件,抓取的数据要⽐较少,不然挺耗时的,其次,有可能遇到验证码拼图,恶⼼,我遇到了,可能爬取频率太⾼,被捕捉到了,不过,你对图⽚处理如果⽐较好,也是挺容易解决的,计算耦合点,以及滑动的距离即可,再次,只能抓到屏幕上存在的所有数据,其余不可以。2.代码实现# -*- encoding=utf8 -*-__author__ = "Mr 雷"from import *from mation import AndroidUiautomationPocoimport csvimport numpy as npauto_setup(__file__)f = open('上海⼆⼿房信息.csv','a+',newline='')writer = (f)ow(['描述','单价','朝向','装修','年代','类型','房本年限','电梯','唯⼀住房','楼层','总价','户型','⾯积','预算','⼩区','商圈'])# 远程设别连接(模拟器)connect_device('android:///emulator-5554?cap_method=javacap&touch_method=adb')poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)xy = _screen_size()x = xy[0]y = xy[1]describes = []# 写⼊⽂件def to_csv(data): ow([('描述',''),('单价',''),('朝向',''), ('装修',''),('年代',''),('类型',''), ('楼层',''),('总价',''),('户',''),('厅',''),('卫',''), ('⾯积', ''),('预算',''),('⼩区',''), ('商圈',''),('轨交','')])# 获取内容def get_info(): father_node = poco(name=':id/home_page_child_recycler_view').wait(10) child_nodes = father_('Layout').wait(10) # 遍历详情节点,进⼊详情页 for node in child_nodes: infos = {} try: ('veLayout').click() except: continue # 等待页⾯缓冲 sleep(() + t(4, 6)) # 如果没有定位到元素,舍弃部分,返回上⼀个页⾯ try: describe = poco(':id/bigTitleTv').get_text() # 如果前⾯采集过这部分信息,不会在重复采集,结束当前循环 if describe in describes: keyevent('KEYCODE_BACK') sleep(() + t(4, 6)) continue total_price = poco(':id/bigPriceDescTv').get_text() room_num = poco(':id/bigHouseTypeRoomNumTv').get_text() hall_num = poco(':id/bigHouseTypeHallNumTv').get_text() toile_num = poco(':id/bigHouseTypeToiletNumTv').get_text() area = poco(':id/bigAreaDescTv').get_text() infos['总价'] = total_price infos['总价'] = total_price infos['室'] = room_num infos['厅'] = hall_num infos['卫'] = toile_num infos['⾯积'] = area infos['描述'] = describe (describe) # 下部分页⾯显⽰ swipe((0.5*x,0.75*y),(0.5*x,0.5*y),duration=()+t(1,3)) infos['单价'] = poco(':id/priceTv').get_text().split(' ')[1] infos['楼层'] = poco(':id/floorTv').get_text().split(' ')[1] infos['朝向'] = poco(':id/orientationTv').get_text().split(' ')[1] infos['类型'] = poco(':id/typeTv').get_text().split(' ')[1] infos['装修'] = poco(':id/decorationTv').get_text().split(' ')[1] infos['年代'] = poco(':id/ageTv').get_text().split(' ')[1] infos['预算'] = poco(':id/budget_content_text_view').get_text() infos['⼩区'] = poco(':id/comm_name_tv').get_text() infos['商圈'] = poco(':id/comm_address_tv').get_text() infos['轨交'] = poco(':id/second_house_metro_text_view').get_text() except Exception as e: print(e) if len(infos): to_csv(infos) keyevent('KEYCODE_BACK') sleep(() + t(4, 6))# 滑动屏幕def swipe_screen(): sleep(1) swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=()+t(3,5))for i in range(0,5000): print(count) get_info() swipe_screen()()3. 结果数据结果.png这是我们市区的⼆⼿房价格,我感觉也已经很⾼了,hhhhh。4. 结语感觉好玩就⼩试⽜⼑,当然了,这个主要是做脚本的,如果感兴趣的话,可以深⼊的,做个游戏脚本刷⾦币、抢红包啥的,都是⾏的通的。⾄此就结束了,有问题可以留⾔交流。

发布者:admin,转转请注明出处:http://www.yc00.com/web/1687985167a63866.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信