2017-07-20 49 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 
    } 
} 
相關問題