首页 > 解决方案 > RSelenium 抓取返回奇怪的结果

问题描述

我正在尝试使用 RSelenium 抓取一些新闻来源搜索页面。这是我的代码:

library(rvest)
library(RSelenium)

#open the browser
rD <- rsDriver(browser=c("chrome"), chromever="73.0.3683.68")
remDr <- rD[["client"]]

#create a blank space to put the links
urlslist_final = list()

##loop through the page number at the end until done with ~1000 / 20 = 50
for (i in 1:2) { ##change this to 50

  url = paste0('https://www.npr.org/search?query=kavanaugh&page=', i)

  #navigate to it
  remDr$navigate(url)

  #get the links
  webElems <- remDr$findElements(using = "css", "[href]")
  urlslist_final[[i]] = unlist(sapply(webElems, function(x) {x$getElementAttribute("href")}))

  #don't go too fast
  Sys.sleep(runif(1, 1, 5))

} #close the loop

remDr$close()
# stop the selenium server
rD[["server"]]$stop()

如果我i = 1在页面导航到之后设置并单击浏览器,那么我会得到 166 个链接的期望结果以及我试图抓取的特定结果链接:

> str(urlslist_final)
List of 1
 $ : chr [1:166] "https://media.npr.org/templates/favicon/favicon-180x180.png" "https://media.npr.org/templates/favicon/favicon-96x96.png" "https://media.npr.org/templates/favicon/favicon-32x32.png" "https://media.npr.org/templates/favicon/favicon-16x16.png" ...

但是,如果只是运行我的循环,我只会得到 91 个结果,而且它们都不是搜索的实际结果:

> str(urlslist_final)
List of 2
$ : chr [1:91] "https://media.npr.org/templates/favicon/favicon-180x180.png" "https://media.npr.org/templates/favicon/favicon-96x96.png" "https://media.npr.org/templates/favicon/favicon-32x32.png" "https://media.npr.org/templates/favicon/favicon-16x16.png" ...

任何帮助理解为什么这里的差异?我能做些什么不同的事情?我尝试只使用 rvest 但我无法找到嵌入在其脚本中的链接以获得结果。

标签: rseleniumweb-scraping

解决方案


感谢我的朋友 Thom,这是一个很好的解决方案:

#scroll on the page
webscroll <- remDr$findElement("css", "body")
webscroll$sendKeysToElement(list(key = "end"))

我在导航到页面和捕获链接之间放置了该代码,这触发了网站认为我正在正确使用它,因此我可以抓取链接。


推荐阅读