如何使用rvest将文本排序到不同的列中?

如何使用rvest将文本排序到不同的列中?,r,rvest,R,Rvest,我正在使用rvest从一个名为RePEc的学术出版物数据库中(尝试)获取所有作者从属关系数据。我有作者的短ID,我用它来收集从属关系数据。但是,每次我尝试时,它都会给我404错误:open.connection中的错误(x,“rb”):HTTP error 404 我使用sapply肯定有问题,因为当我使用单个ID测试它时,它工作正常。以下是我正在使用的代码: df$author_reg <- c("paa6","paa2","paa1", "paa8", "pve266", "pya50

我正在使用
rvest
从一个名为RePEc的学术出版物数据库中(尝试)获取所有作者从属关系数据。我有作者的短ID,我用它来收集从属关系数据。但是,每次我尝试时,它都会给我404错误:
open.connection中的错误(x,“rb”):HTTP error 404

我使用
sapply
肯定有问题,因为当我使用单个ID测试它时,它工作正常。以下是我正在使用的代码:

df$author_reg <- c("paa6","paa2","paa1", "paa8", "pve266", "pya500")

df$websites <- paste0("https://ideas.repec.org/e/", df$author_reg, ".html")

df$affiliation <- sapply(df$websites, function(x) try(x %>% read_html %>% html_nodes("#affiliation h3") %>% html_text()))
df$author\u reg%html\u text())
我实际上需要为六列作者做这件事,我想跳过
NA
值,所以如果有人也知道怎么做,我将非常感激(如果我不知道,那也没什么大不了的)。提前感谢您的帮助


编辑:我刚刚发现错误在网站的公式中。有时应该是
df$网站你可以有这两个链接,并在它们的底部使用
try
。我假设只有1个网站是有效的。否则,我们始终可以编辑代码,以接收所有可用的内容:

library(rvest)
library(purrr)

df = data.frame(id=1:6)

df$author_reg <- c("paa6","paa2","paa1", "paa8", "pve266", "pya500")
http1 <- "https://ideas.repec.org/e/"
http2 <- "https://ideas.repec.org/f/"

df$affiliation <- sapply(df$author_reg, function(x){
  links = c(paste0(http1, x, ".html"),paste0(http2, x, ".html"))

# here we try both links and store under attempt
  attempts = links %>% map(function(i){
    try(read_html(i) %>% html_nodes("#affiliation h3") %>% html_text())
  })

# the good ones will have "character" class, the failed ones, try-error
  gdlink = which(sapply(attempts,class) != "try-error")
  if(length(gdlink)>0){
  return(attempts[[gdlink[1]]])
  }
  else{
  return("True 404 error")
  }
})

根据你提供的例子,我没有得到错误…我意识到公式是错误的。有时应该是“”,有时应该是“”。你知道我怎样才能让它两者都试一下吗?有一个tryCatch机制。很多人都回答了这样的问题。好的,我在下面给你写点东西。。试着看看它是否有效谢谢你这么多,但不幸的是它给了我一个错误消息:“error in
$嘿,我试过你的例子,它对我有效。我可以看到前4个使用“e”,后2个使用“f”。您可以看到更新后的答案嘿,好吧,我重建了数据帧,因为我认为这可能是一个问题,它现在工作得更好,但我得到了这个错误:“尝试中出错[[gdlink]]:尝试在get1index中选择少于一个元素”我想(不确定)有3个死链接。有什么我可以添加到这个代码时,有一个真正的404错误?好的,那么,这个错误是你有一个以上的好链接。。是的,当没有好的链接时,你是对的,比如404错误。如果“e”和“f”都起作用,你会怎么做?如果两个链接都起作用,它们应该是相同的,对吗?你可以退一个吗?好的,请尝试编辑答案。。如果有一个真正的404,它将返回它。否则,它会选择第一个(1或2中)有效的
 df
  id author_reg
1  1       paa6
2  2       paa2
3  3       paa1
4  4       paa8
5  5     pve266
6  6     pya500
                                                                                                                   affiliation
1                                                                                   Statistisk SentralbyråGovernment of Norway
2                                                              Department of EconomicsCollege of BusinessUniversity of Wyoming
3 (80%) Institutt for ØkonomiUniversitetet i Bergen, (20%) Gruppe for trygdeøkonomiInstitutt for ØkonomiUniversitetet i Bergen
4                                                                       Centraal Planbureau (CPB)Government of the Netherlands
5                   Department of FinanceRotterdam School of Management (RSM Erasmus University)Erasmus Universiteit Rotterdam
6                                                                            Business SchoolSwinburne University of Technology