Python+appium+安卓真机实现朋友圈爬⾍
2.⽂件中标注的需要修改的地⽅,
控件id:
请打开android_sdk压缩包,tools⽂件夹uiautomatorviewer.bat双击运⾏,
同时⽤户请⾃⾏将安卓⼿机调到朋友圈界⾯,
点击uiautomatorviewer中左上⾓第⼆个按钮捕捉屏幕按钮,此时安卓机屏幕投影在电脑上,然后点击响应的控件
在右侧树形图中到对应的Node根据右下⽅的Node detail中的resources-id,复制粘贴即可
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdrivermon.by import By
from uch_action import TouchAction
ptions import NoSuchElementException,TimeoutException
import time
花店网页代码html#控件id请修改
shiro是哪个动漫人物PLATFORM = 'Android'
deviceName = 'SM_J7008' #请修改为您的⼿机
app_package = ''
app_activity = '.ui.LauncherUI' #请修改
driver_server = '127.0.0.1:4723/wd/hub'
class Daily():
def __init__(self):
self.desired_caps={
'platformName': PLATFORM,
'deviceName': deviceName,
'appPackage': app_package,
'appActivity': app_activity
}
self.driver = webdriver.Remote(driver_server, self.desired_caps)
self.wait = WebDriverWait(self.driver, 300)
def login(self):
print('点击登陆按钮——————')
login = self.wait.until(EC.presence_of_element_located((By.ID, ':id/ene'))) #控件id
login.click()
# 输⼊⼿机号
phone = self.wait.until(EC.presence_of_element_located((By.ID, ':id/m7')))#控件id
phone_num = input('请输⼊⼿机号')
phone.send_keys(phone_num)
print('点击下⼀步中')
button = self.wait.until(EC.presence_of_element_located((By.ID, ':id/b2f')))#控件id
button.click()
pass_w = input('请输⼊密码:')
password = self.wait.until(EC.presence_of_element_located((By.XPATH, "//android.widget.EditText[@resource-id=':id/m7' and @text='']"))) password.send_keys(pass_w)
login = self.driver.find_element_by_id(':id/b2f')#控件id
login.click()
# 提⽰看看⼿机通讯录否
tip = self.wait.until(EC.element_to_be_clickable((By.ID, ':id/b48')))#控件id
tip.click()
def enter(self):
print('点击发现——')
tab = self.wait.until(EC.element_to_be_clickable((By.XPATH, '//android.widget.RelativeLayout[3]')))
print('已经到发现按钮')
time.sleep(6)
tab.click()
# self.wait._to_be_present_in_element((By.ID,':id/cdj'),'发现'))
print('点击朋友圈')
friends = self.wait.until(EC.presence_of_element_located(
(By.XPATH, '//*[@resource-id="android:id/list"]/*[@class="android.widget.LinearLayout"][1]')))
friends.click()
弱电switch是什么意思def crawl_pic(self,id):
print("开始抓取朋友圈动态")
while True:
# try:
# pics = WebDriverWait(self.driver, 10).until(EC.presence_of_all_elements_located((By.ID, id)))
# finally:
# self.driver.swipe(300, 750, 300, 200)
try:
pics = WebDriverWait(self.driver, 10).until(
EC.presence_of_all_elements_located((By.ID, id))
)
#print(pics)
except (NoSuchElementException,TimeoutException):
print("元素不存在")
self.driver.swipe(300, 750, 300, 200)
爬虫软件 appcontinue
else:
# pics = self.driver.find_elements_by_id(id)
#pics = WebDriverWait(self.driver, 10).until(EC.presence_of_all_elements_located((By.ID, id)))
print("当前页⾯有",len(pics),"个图⽚1")
#time.sleep(7)
for pic in pics:
try:
print("----点击图⽚---")
pic.click()
time.sleep(2)
el = self.driver.find_element_by_id(":id/ah7")
TouchAction(self.driver).long_press(el).perform().release()
time.sleep(3)
save = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@resource-id=":id/mg"][3]'))) #控件id print("----保存图⽚---")
save.click()
time.sleep(3)
print("已存⼊⼿机相册")
TouchAction(self.driver).tap(el).perform().release()
time.sleep(1)
# pic_ret = self.wait.until(EC.presence_of_element_located((By.ID, ':id/ah7'))) #c8a
print("----下⼀张---")
except BaseException as e:
print(e)
print("该页⾯爬取完毕")
self.driver.swipe(300, 750, 300, 200)
def crawl_video(self):
print("开始抓取朋友圈动态")
while True:
try:
videos = WebDriverWait(self.driver, 10).until(
EC.presence_of_all_elements_located((By.ID, ':id/atb'))#控件id
)
#print(videos)
except (NoSuchElementException,TimeoutException):
print("元素不存在")
self.driver.swipe(300, 750, 300, 200)
continue
else:
for video in videos:
try:
#判断昨天的内容停⽌下载
time_txt = self.driver.find_element_by_id(":id/eza")#控件id
if(=='昨天'):
#结束
print("今天的内容已下载完毕。",)
doubleclick = self.driver.find_element_by_id(":id/kx")
TouchAction(self.driver).tap(doubleclick,count=2).perform().release()
return
print("----点击视频---")
video.click()
time.sleep(2)
el = self.driver.find_element_by_id(":id/f5s")#控件id
TouchAction(self.driver).long_press(el).perform()
time.sleep(3)
save = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@resource-id=":id/mg"][3]'))) #控件id print("----保存视频---")
save.click()
time.sleep(3)
print("已存⼊⼿机相册")
不能在dropdownlist中选择多个项video_ret = self.wait.until(EC.presence_of_element_located((By.ID, ':id/ah5'))) #控件id
print("----下⼀张---")
video_ret.click()
time.sleep(1)
except BaseException as e:
print(e)
self.driver.swipe(300, 750, 300, 300)
def main(self):
self.login()#登陆
<()#进⼊朋友圈
D = Daily()
D.main()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论