使用 purrr:map 循环浏览网页以使用 Rselenium 进行抓取

Using purrr:map to loop through web pages for scraping with Rselenium

我有一个基本的 R 脚本,我使用 Rselenium 拼凑而成,它允许我登录网站,一旦验证我的脚本,然后转到感兴趣的第一页并从页面中提取 3 段文本。

对我来说幸运的是 URL 的创建方式使我可以将数字向量传递给 URL 以将我带到感兴趣的下一页,因此使用地图()。

虽然在每个页面上我想从页面上抓取相同的 3 个元素并将它们存储在主数据框中以供以后分析。

我希望使用 map 系列函数,以便我可以更加熟悉它们,但我真的很难让它们发挥作用,谁能告诉我哪里出错了?

这是我的代码的主要部分(转到网站并登录)

library(RSelenium)
# 
rd <- rsDriver(browser = "chrome",
               chromever = "88.0.4324.27",
               port = netstat::free_port())

remdr <- rd[["client"]]

# url of the site's login page
url <- "https://www.myWebsite.com/"

# Navigating to the page
remdr$navigate(url)

# Wait 5 secs for the page to load
Sys.sleep(5)

# Find the initial login button to bring up the username and password fields
loginbutton <- remdr$findElement(using = 'css selector','.plain')

# Click the initial login button to bring up the username and password fields
loginbutton$clickElement()

# Find the username box
username <- remdr$findElement(using = 'css selector','#username')

# Find the password box
password <- remdr$findElement(using = 'css selector','#password')

# Find the final login button
login <- remdr$findElement(using = 'css selector','#btnLoginSubmit1')

# Input the username
username$sendKeysToElement(list("myUsername"))

# Input the password
password$sendKeysToElement(list("myPassword"))

# Click login
login$clickElement()

嘿,我们来了!

现在我的代码将我带到感兴趣的初始页面(索引 = 1)

上面我提到我希望增加每一页,我可以通过将整数替换为 rcId 元素的 URL 来实现,见下文

#remdr$navigate("https://myWebsite.com/rc_redesign/#/layout/jcard/drugCard?accountId=XXXXXX&rcId=1&searchType=R&reimbCode= &searchTerm=&searchTexts=*") # 导航到页面

对于1:9999中的每个rcId,我希望获取以下3个元素并将它们存储在数据框中

hcpcs_info <- remdr$findElement(using = 'class','is-jcard-heading')

hcpcs <- hcpcs_info$getElementText()[[1]]

hcpcs_description <- remdr$findElement(using = 'class','is-jcard-desc')

hcpcs_desc <- hcpcs_description$getElementText()[[1]]

tc_info <- remdr$findElement(using = 'css selector','.col-12.ng-star-inserted')

therapeutic_class <- tc_info$getElementText()[[1]]

我试过创建一个单独的函数并传递给 map 但我还不够先进,无法将它们拼凑起来,下面是我尝试过的方法。

my_function <- function(index) {
  remdr$navigate(sprintf("https://rc2.reimbursementcodes.com/rc_redesign/#/layout/jcard/drugCard?accountId=113479&rcId=%d&searchType=R&reimbCode=*&searchTerm=*&searchTexts=*",index)
                 Sys.sleep(5)
                 hcpcs_info[index] <- remdr$findElement(using = 'class','is-jcard-heading')
                 hcpcs[index] <- hcpcs_info$getElementText()[index][[1]])
}

x <- 1:10 %>% 
map(~ my_function(.x))

如有任何帮助,我们将不胜感激

尝试以下操作:

library(RSelenium)

purrr::map_df(1:10, ~{
          remdr$navigate(sprintf("https://rc2.reimbursementcodes.com/rc_redesign/#/layout/jcard/drugCard?accountId=113479&rcId=%d&searchType=R&reimbCode=*&searchTerm=*&searchTexts=*",.x))
          Sys.sleep(5)
          hcpcs_info <- remdr$findElement(using = 'class','is-jcard-heading')
          hcpcs <- hcpcs_info$getElementText()[[1]]
          hcpcs_description <- remdr$findElement(using = 'class','is-jcard-desc')
          hcpcs_desc <- hcpcs_description$getElementText()[[1]]
          tc_info <- remdr$findElement(using = 'css selector','.col-12.ng-star-inserted')
          therapeutic_class <- tc_info$getElementText()[[1]]
          tibble(hcpcs, hcpcs_desc, therapeutic_class)
          }) -> result
result