微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 R. Web 抓取 Google Play 中按下带有 RSelenium 的按钮

如何解决在 R. Web 抓取 Google Play 中按下带有 RSelenium 的按钮

我正在尝试抓取 Google Play 上的所有应用评论。问题是在加载评论页面向下滚动 4 次后,会出现“显示更多”按钮,但我不知道如何按下它并再次运行 4 次,依此类推,直到到达页面底部

我的“向下滚动”代码如下。他把它加载但它不起作用,因为在向下滚动 4 次后出现“显示更多”按钮,我不知道如何在代码中按下它。

    #Specifying the url for desired website to be scrapped
  url <- list('https://play.google.com/store/apps/details?id=com.nianticlabs.pokemongo&showAllReviews=true')

  # starting local RSelenium (this is the only way to start RSelenium that is working for me atm)
  selCommand <- wdman::selenium(jvmargs = c("-Dwebdriver.chrome.verboseLogging=true"),retcommand = TRUE)
  shell(selCommand,wait = FALSE,minimized = TRUE)
  remDr <- remoteDriver(port = 4567L,browserName = "firefox")
  remDr$open()

  # go to website
  remDr$navigate(url)

  
  replicate(1000,{
            # scroll down
            webElem <- remDr$findElement("css","body")
            webElem$sendKeystoElement(list(key = "end"))
            # wait
            Sys.sleep(4)
          })
 
  
  morereviews <- remDr$findElement(using = 'css selector',".RveJvd.snByac")
  # click button
  morereviews$clickElement()

你能帮我吗?

解决方法

一种可能的解决方案是使用 class name 选择器。

这对我有用。

library(RSelenium)
driver <- rsDriver(browser= 'firefox',port = 4551L)
remote_driver <- driver[["client"]] 
url1 <- 'https://play.google.com/store/apps/details?id=com.nianticlabs.pokemongo&showAllReviews=true'
remote_driver$navigate(url1)

#You can custom the range of the for if you want

    for (i in 1:10) {
    replicate(4,#4 is the correct value of the scroll in this example
              {
                # scroll down
                webElem <- remote_driver$findElement("css","body")
                webElem$sendKeysToElement(list(key = "end"))
                # wait
                Sys.sleep(4)
              })
      
    Sys.sleep(2) 
    remote_driver$findElement(using = 'class name',value = 'CwaK9')$clickElement()
    
    }

如果你想实现关于“显示更多”的 tryCatch 函数,下面是一个可能的解决方案。

  replicate(4,{
            webElem <- remote_driver$findElement("css","body")
            webElem$sendKeysToElement(list(key = "end"))
            Sys.sleep(4)
            tryCatch(expr = { 
                  remote_driver$findElement(using = 'class name',value = 
                 'CwaK9')$clickElement()
              },error = function(e){
                       message("no find")
                                  }
                    )             
              })

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。