Html R-使用rvest刮取受密码保护的网站,而无需在每次循环迭代时登录

Html R-使用rvest刮取受密码保护的网站,而无需在每次循环迭代时登录,html,r,web-scraping,rvest,Html,R,Web Scraping,Rvest,我正在尝试使用rvest包从R中的一个受密码保护的网站上抓取数据。我的代码目前在循环的每次迭代中登录到该网站,循环将运行大约15000次。这似乎效率很低,但我还没有找到解决办法,因为每次返回到网站的登录页面时,都不用先登录就跳转到不同的url。我的代码简化如下: library(rvest) url <- password protected website url within quotes session <-html_session(url) form <-html_fo

我正在尝试使用rvest包从R中的一个受密码保护的网站上抓取数据。我的代码目前在循环的每次迭代中登录到该网站,循环将运行大约15000次。这似乎效率很低,但我还没有找到解决办法,因为每次返回到网站的登录页面时,都不用先登录就跳转到不同的url。我的代码简化如下:

library(rvest)
url <- password protected website url within quotes
session <-html_session(url)
form <-html_form(session)[[1]]

filled_form <- set_values(form,
                      `username` = email within quotes, 
                      `password` = password within quotes)
start_table <- submit_form(session, filled_form) %>%
  jump_to(url from which to scrape first table within quotes) %>%
  html_node("table.inlayTable") %>%
  html_table()
data_table <- start_table

for(i in 1:nrow(data_ids))
{
current_table <- try(submit_form(session, filled_form) %>%
  jump_to(paste(first part of url within quotes, data_ids[i, ], last part of url within quotes, sep="")) %>%
  html_node("table.inlayTable") %>%
  html_table())

data_table <- rbind(data_table, current_table)
}
库(rvest)

url您可以将会话保存在变量中,但我想您不会节省那么多时间。 以下是我的网页抓取脚本:

library(rvest)
url <- "https://"
session <- html_session(url)              
form <- html_form(session)[[1]]

filled_form <- set_values(form,`[login]` = "xxx",`[password]` = "xxx")

session <- submit_form(session,filled_form)

for (i in unique(id)) {
      link <- paste0("https://",i,"xxx")
      df_all <- session %>% jump_to(link) %>% html_table()
        if ( length(df_all) != 0 ) {
          my_df <- as.data.frame(df_all[n],optional = TRUE)
          database <- rbind(my_df,database)
          cat("Data saved for",i)
          } else {
          cat("No data for",i)
          }
         }
库(rvest)
网址