跳过错误 open.connection(x, "rb") 中的错误:HTTP 错误 404
skip error Error in open.connection(x, "rb") : HTTP error 404
您好,我是r这个迷人世界的新手,我一直无法跳过不存在的url,我该如何处理?并且不要标记为错误,感谢您的帮助。
标题:“错误”
作者:“FJSG”
日期:“27/6/2020”
输出:html_document
knitr::opts_chunk$set(echo = TRUE)
library(xml2)
library(rvest)
library(tidyverse)
library(lubridate)
zora_core <- read_html("https://zora.medium.com/the-zora-music-canon-5a29296c6112")
Los_100 <- data.frame(album = html_nodes(zora_core, "h1:not(#96c9)") %>%
html_text() %>%
str_trim(side = "both"),
interprete = html_nodes(zora_core, "strong em , p#73e0 strong") %>%
html_text() %>%
str_remove_all("^by") %>%
str_extract("[a-zA-Z].+(?=[(])") %>% str_trim(side = "both"),
año = html_nodes(zora_core, "strong em , p#73e0 strong") %>%
html_text %>%
str_extract("([[:digit:]]){4}"),
liga = paste0("https://en.wikipedia.org/wiki/",html_nodes(zora_core, "strong em , p#73e0 strong") %>%
html_text() %>%
str_remove_all("^by") %>%
str_extract("[a-zA-Z].+(?=[(])") %>% str_trim(side = "both") %>% str_replace_all(" ","_")))
carga <- function(url){
perfil_raw <- read_html(url)
data.frame(interprete = html_node(perfil_raw, "h1#firstHeading") %>%
html_text() %>% str_trim(side = "both"))
}
lista <- Los_100$liga[1:16] # THE url for the position 16 don´t exist how to avoid that
datos_personales <- map_df(lista,carga)
了解 R 中的错误处理很有用,但在处理 http 请求时它变得必不可少。
在您的情况下,最好将 carga
包裹在 tryCatch
中。这将运行您作为第一个参数传递的表达式,如果抛出错误,它会被捕获并传递给 tryCatch
的第二个参数,这是一个函数。
如果抛出错误,我们需要 return 一个包含名为 interprete
的单列的数据框,以便 map_df
可以将其与其他结果绑定在一起:
carga_catch <- function(x)
{
tryCatch(return(carga(x)),
error = function(e) return(data.frame(interprete = "**inexistente**")))
}
map_df(lista, carga_catch)
#> interprete
#> 1 Ella Fitzgerald
#> 2 Sarah Vaughan
#> 3 Billie Holiday
#> 4 Sister Rosetta Tharpe
#> 5 Lena Horne
#> 6 Mahalia Jackson
#> 7 Abbey Lincoln
#> 8 Etta James
#> 9 Leontyne Price
#> 10 Marian Anderson
#> 11 Dinah Washington
#> 12 Odetta
#> 13 Dionne Warwick
#> 14 The Supremes
#> 15 Nina Simone
#> 16 **inexistente**
除了错误处理之外,我认为您的代码对于刚开始使用 R 的人来说非常好。它在几行代码中实现了很多,并且非常可读。干得好!
您好,我是r这个迷人世界的新手,我一直无法跳过不存在的url,我该如何处理?并且不要标记为错误,感谢您的帮助。
标题:“错误” 作者:“FJSG” 日期:“27/6/2020” 输出:html_document
knitr::opts_chunk$set(echo = TRUE) library(xml2) library(rvest) library(tidyverse) library(lubridate) zora_core <- read_html("https://zora.medium.com/the-zora-music-canon-5a29296c6112") Los_100 <- data.frame(album = html_nodes(zora_core, "h1:not(#96c9)") %>% html_text() %>% str_trim(side = "both"), interprete = html_nodes(zora_core, "strong em , p#73e0 strong") %>% html_text() %>% str_remove_all("^by") %>% str_extract("[a-zA-Z].+(?=[(])") %>% str_trim(side = "both"), año = html_nodes(zora_core, "strong em , p#73e0 strong") %>% html_text %>% str_extract("([[:digit:]]){4}"), liga = paste0("https://en.wikipedia.org/wiki/",html_nodes(zora_core, "strong em , p#73e0 strong") %>% html_text() %>% str_remove_all("^by") %>% str_extract("[a-zA-Z].+(?=[(])") %>% str_trim(side = "both") %>% str_replace_all(" ","_")))
carga <- function(url){ perfil_raw <- read_html(url) data.frame(interprete = html_node(perfil_raw, "h1#firstHeading") %>% html_text() %>% str_trim(side = "both")) }
lista <- Los_100$liga[1:16] # THE url for the position 16 don´t exist how to avoid that datos_personales <- map_df(lista,carga)
了解 R 中的错误处理很有用,但在处理 http 请求时它变得必不可少。
在您的情况下,最好将 carga
包裹在 tryCatch
中。这将运行您作为第一个参数传递的表达式,如果抛出错误,它会被捕获并传递给 tryCatch
的第二个参数,这是一个函数。
如果抛出错误,我们需要 return 一个包含名为 interprete
的单列的数据框,以便 map_df
可以将其与其他结果绑定在一起:
carga_catch <- function(x)
{
tryCatch(return(carga(x)),
error = function(e) return(data.frame(interprete = "**inexistente**")))
}
map_df(lista, carga_catch)
#> interprete
#> 1 Ella Fitzgerald
#> 2 Sarah Vaughan
#> 3 Billie Holiday
#> 4 Sister Rosetta Tharpe
#> 5 Lena Horne
#> 6 Mahalia Jackson
#> 7 Abbey Lincoln
#> 8 Etta James
#> 9 Leontyne Price
#> 10 Marian Anderson
#> 11 Dinah Washington
#> 12 Odetta
#> 13 Dionne Warwick
#> 14 The Supremes
#> 15 Nina Simone
#> 16 **inexistente**
除了错误处理之外,我认为您的代码对于刚开始使用 R 的人来说非常好。它在几行代码中实现了很多,并且非常可读。干得好!