统计房价

# coding:utf-8

import uiautomator2 as u2
import time

import re

import os
import subprocess
import shutil

# adb_connect_init
cmds = [
"taskkill /f /im adb.exe",
'start adb',
'nox_adb connect 127.0.0.1:62001',
"adb connect 127.0.0.1:62001",
"adb devices",
]


for cmd in cmds:
print(10*"--")
result = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
print(result.stdout.read().decode("gbk"))
time.sleep(2)

# 清空screenshoot文件夹
if os.path.exists('./screenshoot'):
shutil.rmtree('./screenshoot')
os.mkdir('./screenshoot')
else:
os.mkdir('./screenshoot')

# 连接APP
d = u2.connect('127.0.0.1:62001')

# 关闭之前旧的APP
# 停止所有正在进行的app
d.app_stop_all()


# 启动APP
d.app_start("com.lianjia.beike")
time.sleep(5)
print('开始。。。')
time.sleep(5) # 等5秒钟


# 输入搜索的楼盘
d(resourceId="com.lianjia.beike:id/a01").click() # 输入之前对输入框进行click()操作,用来获取焦点
time.sleep(2) # 等2秒钟
d.set_fastinput_ime(True) # 切换成FastInputIME输入法
d.send_keys("保利学府里") # adb广播输入
d.set_fastinput_ime(False) # 切换成正常的输入法
time.sleep(2) # 等2秒钟
d(text="小区").click()
time.sleep(2) # 等2秒钟
d(text="成交").click()
time.sleep(2) # 等2秒钟

# search_result = d.xpath('//androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout[1]')
#
# tv_house_title = search_result.child(resourceId='com.bk.secondhouse:id/tv_house_title')
#
# print(str(tv_house_title))

search_results = []

 

while True:

# 每个搜索结果模块的结构
house_title = d(resourceId = 'com.bk.secondhouse:id/tv_house_title')
house_info = d(resourceId = 'com.bk.secondhouse:id/tv_house_info')
sold_time = d(resourceId = 'com.bk.secondhouse:id/tv_sold_time')

tv_house_price = d(resourceId = 'com.bk.secondhouse:id/tv_house_price')
tv_house_price_unit = d(resourceId = 'com.bk.secondhouse:id/tv_house_price_unit')
tv_house_avgprice = d(resourceId = 'com.bk.secondhouse:id/tv_house_avgprice')

n = len(house_title)
print(n)
for i in range(n):
print(house_title[i].get_text())
print(house_info[i].get_text())
print(sold_time[i].get_text())
print(tv_house_price[i].get_text()+tv_house_price_unit[i].get_text()+tv_house_avgprice[i].get_text())
print('--' * 10)


# 上划一下
d.swipe(0.5, 0.8, 0.5, 0.3)
time.sleep(2)

to_end = d(text='没有更多啦').exists # 是否已到底了

# 要考虑到底时还要执行一次

flag = False
if (to_end == True & flag == True): # 到底了 且 标志 = True, 才退出循环
break
else:
pass

if to_end == True:
flag = True

posted @ 2021-07-30 15:43  cforcnb  阅读(34)  评论(0)    收藏  举报