检查是否可以使用RSelenium向下滚动 [英] Check if it's possible to scroll down with RSelenium

查看:96
本文介绍了检查是否可以使用RSelenium向下滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RSelenium自动向下滚动社交媒体网站并保存帖子.有时,我到达网页的底部,由于没有更多数据可用,因此无法加载更多帖子.我只是想能够检查是否是这种情况,所以我可以停止尝试滚动.

I'm using RSelenium to automatically scroll down a social media website and save posts. Sometimes I get to the bottom of the webpage and no more posts can be loaded as no more data is available. I just want to be able to check if this is the case so I can stop trying to scroll.

如何确定是否可以继续在RSelenium中滚动?下面的代码说明了我正在尝试做的事情-我认为我只需要"if"语句的帮助.

How can I tell if it's possible to continue scrolling in RSelenium? The code below illustrates what I'm trying to do - I think I just need help with the "if" statement.

仅供参考,有一种解决方案可以在Python中使用

FYI there's a solution for doing this in Python here (essentially checking if page height changes between iterations), but I can't figure out how to implement it (or any another solution) in R.

# Open webpage
library(RSelenium)
rD = rsDriver(browser = "firefox")
remDr = rD[["client"]]
url = "https://stocktwits.com/symbol/NZDCHF"
remDr$navigate(url) 

# Keep scrolling down page, loading new content each time. 
ptm = proc.time()
repeat {   
  remDr$executeScript("window.scrollTo(0,document.body.scrollHeight);")
  Sys.sleep(3) #delay by 3sec to give chance to load. 

  # Here's where i need help  
  if([INSERT CONDITION TO CHECK IF SCROLL DOWN IS POSSIBLE]) {
    break
  }
}

推荐答案

在Python中偶然发现了一种方法此处,并对其进行了修改,使其可以在R中使用.下面是我上面发布的原始代码的最新更新.

Stumbled across a way to do this in Python here and modified it to work in R. Below is a now-working update of the original code I posted above.

# Open webpage
library(RSelenium)
rD = rsDriver(browser = "firefox")
remDr = rD[["client"]]
url = "https://stocktwits.com/symbol/NZDCHF"
remDr$navigate(url) 

# Keep scrolling down page, loading new content each time. 
last_height = 0 #
repeat {   
  remDr$executeScript("window.scrollTo(0,document.body.scrollHeight);")
  Sys.sleep(3) #delay by 3sec to give chance to load. 

  # Updated if statement which breaks if we can't scroll further 
  new_height = remDr$executeScript("return document.body.scrollHeight")
  if(unlist(last_height) == unlist(new_height)) {
    break
  } else {
    last_height = new_height
  }
}

这篇关于检查是否可以使用RSelenium向下滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆