使用时检索h1标题时rvest,我有时会遇到404页.这将停止该过程并返回此错误.
open.connection(x,"rb")出错:HTTP错误404.
请参阅下面的示例
Data<-data.frame(Pages=c(
"http://boingboing.net/2016/06/16/spam-king-sanford-wallace.html",
"http://boingboing.net/2016/06/16/omg-the-japanese-trump-commer.html",
"http://boingboing.net/2016/06/16/omar-mateen-posted-to-facebook.html",
"http://boingboing.net/2016/06/16/omar-mateen-posted-to-facdddebook.html"))
Run Code Online (Sandbox Code Playgroud)
用于检索h1的代码
library (rvest)
sapply(Data$Pages, function(url){
url %>%
as.character() %>%
read_html() %>%
html_nodes('h1') %>%
html_text()
})
Run Code Online (Sandbox Code Playgroud)
有没有办法包含一个参数来忽略错误并继续这个过程?
你正在寻找try或者tryCatch,R是如何处理错误捕获的.
有了try,你只需要包装可能失败的东西,try()它将返回错误并继续运行:
library(rvest)
sapply(Data$Pages, function(url){
try(
url %>%
as.character() %>%
read_html() %>%
html_nodes('h1') %>%
html_text()
)
})
# [1] "'Spam King' Sanford Wallace gets 2.5 years in prison for 27 million Facebook scam messages"
# [2] "OMG, this Japanese Trump Commercial is everything"
# [3] "Omar Mateen posted to Facebook during Orlando mass shooting"
# [4] "Error in open.connection(x, \"rb\") : HTTP error 404.\n"
Run Code Online (Sandbox Code Playgroud)
然而,虽然这将获得一切,但它也会在我们的结果中插入不良数据.tryCatch允许您配置在调用错误时发生的事情,方法是将该函数传递给该条件时运行的函数:
sapply(Data$Pages, function(url){
tryCatch(
url %>%
as.character() %>%
read_html() %>%
html_nodes('h1') %>%
html_text(),
error = function(e){NA} # a function that returns NA regardless of what it's passed
)
})
# [1] "'Spam King' Sanford Wallace gets 2.5 years in prison for 27 million Facebook scam messages"
# [2] "OMG, this Japanese Trump Commercial is everything"
# [3] "Omar Mateen posted to Facebook during Orlando mass shooting"
# [4] NA
Run Code Online (Sandbox Code Playgroud)
我们去; 好多了.
在tidyverse,该purrr包提供了两个功能,safely并且possibly,这工作就像try和tryCatch.它们是副词,而不是动词,意味着它们接受一个函数,修改它以便处理错误,并返回一个可以调用的新函数(不是数据对象).例:
library(tidyverse)
library(rvest)
df <- Data %>% rowwise() %>% # Evaluate each row (URL) separately
mutate(Pages = as.character(Pages), # Convert factors to character for read_html
title = possibly(~.x %>% read_html() %>% # Try to take a URL, read it,
html_nodes('h1') %>% # select header nodes,
html_text(), # and collect text inside.
NA)(Pages)) # If error, return NA. Call modified function on URLs.
df %>% select(title)
## Source: local data frame [4 x 1]
## Groups: <by row>
##
## # A tibble: 4 × 1
## title
## <chr>
## 1 'Spam King' Sanford Wallace gets 2.5 years in prison for 27 million Facebook scam messages
## 2 OMG, this Japanese Trump Commercial is everything
## 3 Omar Mateen posted to Facebook during Orlando mass shooting
## 4 <NA>
Run Code Online (Sandbox Code Playgroud)