Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:通过长时间但有限的滚动直到结束(使用RSelenium?)_R_Web Scraping_Rvest_Rselenium_Dynamic Loading - Fatal编程技术网

R:通过长时间但有限的滚动直到结束(使用RSelenium?)

R:通过长时间但有限的滚动直到结束(使用RSelenium?),r,web-scraping,rvest,rselenium,dynamic-loading,R,Web Scraping,Rvest,Rselenium,Dynamic Loading,几乎是无限的,因为它在一个动态加载页面中显示了超过6000个配置文件 仅显示310个配置文件,因此滚动到其末尾不需要太多时间 有没有一种方法可以编写一个代码,通过滚动到最后来刮去两个页面 出于类似的目的,我使用了一个带有RSelenium的代码,如下所示: journal_url <- "https://www.frontiersin.org/journals/photonics#editorial-board" rD <- RSelenium::rsDrive

几乎是无限的,因为它在一个动态加载页面中显示了超过6000个配置文件

仅显示310个配置文件,因此滚动到其末尾不需要太多时间

有没有一种方法可以编写一个代码,通过滚动到最后来刮去两个页面

出于类似的目的,我使用了一个带有
RSelenium
的代码,如下所示:

journal_url <- "https://www.frontiersin.org/journals/photonics#editorial-board"

rD <- RSelenium::rsDriver(browser="chrome", port=4546L, verbose=F, chromever="87.0.4280.20")
    
for(i in 1:5){      
    remDr$executeScript(paste("scroll(0,",i*10000,");"))
    Sys.sleep(3)    
}

journal\u url我相信你会在这里找到答案

在副标题下“向下滚动到末尾(如果页面太多,则不建议这样做)。”

编辑:下面是链接中的建议代码

element <- driver$findElement("css", "body")
flag <- TRUE
counter <- 0
n <- 5
while(flag){
    counter <- counter + 1
    #compare the pagesource every n(n=5) time, since sometimes one scroll down doesn't render new content
    for(i in 1:n){
        element$sendKeysToElement(list("key"="page_down"))
        Sys.sleep(2)
    }
    if(exists("pagesource")){
        if(pagesource == driver$getPageSource()[[1]]){
            flag <- FALSE
            writeLines(paste0("Scrolled down ",n*counter," times.\n"))
        } else {
            pagesource <- driver$getPageSource()[[1]]
        }
    } else {
        pagesource <- driver$getPageSource()[[1]]
    }
}

element我相信你会在这里找到答案

在副标题下“向下滚动到末尾(如果页面太多,则不建议这样做)。”

编辑:下面是链接中的建议代码

element <- driver$findElement("css", "body")
flag <- TRUE
counter <- 0
n <- 5
while(flag){
    counter <- counter + 1
    #compare the pagesource every n(n=5) time, since sometimes one scroll down doesn't render new content
    for(i in 1:n){
        element$sendKeysToElement(list("key"="page_down"))
        Sys.sleep(2)
    }
    if(exists("pagesource")){
        if(pagesource == driver$getPageSource()[[1]]){
            flag <- FALSE
            writeLines(paste0("Scrolled down ",n*counter," times.\n"))
        } else {
            pagesource <- driver$getPageSource()[[1]]
        }
    } else {
        pagesource <- driver$getPageSource()[[1]]
    }
}

element大家好,欢迎光临!虽然你的答案很好,但如果你在答案中引用部分外部网页,通常效果最好。链接有一种在将来不起作用的趋势,引用相关部分有助于保持你的答案的相关性,即使链接停止工作。它工作得很好,非常感谢你,克里斯托弗!我只想说,做
(“key”=“end”)
(“key”=“page\u down”)
更快更好,因为
“key”=“end”
直接滚动到页面底部。大家好,欢迎光临!虽然你的答案很好,但如果你在答案中引用部分外部网页,通常效果最好。链接有一种在将来不起作用的趋势,引用相关部分有助于保持你的答案的相关性,即使链接停止工作。它工作得很好,非常感谢你,克里斯托弗!我只想指出,做
(“key”=“end”)
(“key”=“page\u down”)
更快更好,因为
“key”=“end”
直接滚动到页面底部。