gpt4 book ai didi

r - 使用tryCatch和rvest处理404等爬行错误

转载 作者:行者123 更新时间:2023-12-01 18:35:13 25 4
gpt4 key购买 nike

当使用 rvest 检索 h1 标题时,我有时会遇到 404 页面。这会停止进程并返回此错误。

Error in open.connection(x, "rb") : HTTP error 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"))

用于检索 h1 的代码

library (rvest)
sapply(Data$Pages, function(url){
url %>%
as.character() %>%
read_html() %>%
html_nodes('h1') %>%
html_text()
})

有没有办法包含一个参数来忽略错误并继续该过程?

最佳答案

您正在寻找 trytryCatch,这是 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"

然而,虽然这会得到一切,但它也会将错误的数据插入到我们的结果中。 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

我们开始了;好多了。

<小时/>

更新

在 tidyverse 中,purrr 包提供了两个函数,safelypossously,其工作方式类似于 trytryCatch。它们是副词,而不是动词,这意味着它们接受一个函数,修改它以处理错误,并返回一个可以调用的新函数(不是数据对象)。示例:

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>

关于r - 使用tryCatch和rvest处理404等爬行错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38114066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com