检查是否可以使用 RSelenium 向下滚动
Check if it's possible to scroll down with RSelenium
我正在使用 RSelenium 自动向下滚动社交媒体网站并保存帖子。有时我到达网页底部,由于没有更多数据可用,无法加载更多帖子。我只想检查是否是这种情况,这样我就可以停止尝试滚动。
如何判断是否可以在 RSelenium 中继续滚动?下面的代码说明了我正在尝试做的事情——我想我只需要 "if" 语句的帮助。
仅供参考,在 Python here 中有一个解决方案(本质上是检查页面高度是否在迭代之间发生变化),但我不知道如何实现它(或任何其他解决方案) ) 在 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 here 中偶然发现了一种方法,并将其修改为在 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.
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 自动向下滚动社交媒体网站并保存帖子。有时我到达网页底部,由于没有更多数据可用,无法加载更多帖子。我只想检查是否是这种情况,这样我就可以停止尝试滚动。
如何判断是否可以在 RSelenium 中继续滚动?下面的代码说明了我正在尝试做的事情——我想我只需要 "if" 语句的帮助。
仅供参考,在 Python here 中有一个解决方案(本质上是检查页面高度是否在迭代之间发生变化),但我不知道如何实现它(或任何其他解决方案) ) 在 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 here 中偶然发现了一种方法,并将其修改为在 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.
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
}
}