2017-07-20 46 views
1

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

如何判断是否可以继续在RSelenium中滚动?下面的代码说明了我正在尝试做什么 - 我想我只需要“if”语句的帮助。

FYI有用于在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 
    } 
} 
弄清楚如何实现它(或任何其他解决方案)

回答

2

在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 
    } 
} 
相关问题