Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
tryCatch函数适用于大多数不存在的URL,但(至少)在一种情况下不起作用_R_Url_Web Scraping_Try Catch - Fatal编程技术网

tryCatch函数适用于大多数不存在的URL,但(至少)在一种情况下不起作用

tryCatch函数适用于大多数不存在的URL,但(至少)在一种情况下不起作用,r,url,web-scraping,try-catch,R,Url,Web Scraping,Try Catch,亲爱的Stackoverflow用户: 我用R从今天的心理学中搜集一些心理治疗师的资料;这样做是为了锻炼和学习更多关于网页抓取的知识 我是R的新手,我必须经历这场激烈的训练,这将帮助我完成未来的项目。这意味着我可能不知道我目前正在做什么(例如,我可能无法很好地解释脚本或来自R的错误消息),但我必须完成它。因此,请原谅可能的误解或不准确之处 简言之,情况如下。 我已经创建了一个函数,通过它我可以从心理治疗师档案的2个节点中获取信息;该功能显示在该屏幕上 然后我创建了一个循环,在这个循环中,该函数用

亲爱的Stackoverflow用户:

我用R从今天的心理学中搜集一些心理治疗师的资料;这样做是为了锻炼和学习更多关于网页抓取的知识

我是R的新手,我必须经历这场激烈的训练,这将帮助我完成未来的项目。这意味着我可能不知道我目前正在做什么(例如,我可能无法很好地解释脚本或来自R的错误消息),但我必须完成它。因此,请原谅可能的误解或不准确之处

简言之,情况如下。 我已经创建了一个函数,通过它我可以从心理治疗师档案的2个节点中获取信息;该功能显示在该屏幕上

然后我创建了一个循环,在这个循环中,该函数用于一些心理治疗师的个人资料;循环也在上面的文章中,但我在下面报告它,因为这是脚本中产生一些问题的部分(除了我在上面提到的文章中解决的问题之外)


j是的,您需要在
read\u html
调用周围包装一个
tryCatch
。这是R尝试连接到网站的地方,因此如果连接失败,它将抛出一个错误(而不是返回一个空对象)。您可以捕获该错误,然后使用
next
告诉R跳到循环的下一个迭代

library(rvest)
##Valid URL, works fine
URL <- "https://news.bbc.co.uk"
read_html(URL)

##Invalid URL, error raised
URL <- "https://news.bbc.co.uk/not_exist"
read_html(URL)
##Leads to error
Error in open.connection(x, "rb") : HTTP error 404.

##Invalid URL, catch and skip to next iteration of the loop
URL <- "https://news.bbc.co.uk/not_exist"
tryCatch({
URL <- read_html(URL)},
error=function(e) {print("URL Not Found, skipping")
                  next})
库(rvest)
##有效的URL,工作正常

URL我要感谢@Jul的回答。 在这里我发布了我的更新循环:

  j <- 1
MHP_codes <-  c(150000:150200) #therapist identifier
df_list <- vector(mode = "list", length(MHP_codes))
for(code1 in MHP_codes) {
  delayedAssign("do.next", {next})
  URL <- paste0('https://www.psychologytoday.com/us/therapists/illinois/', code1)
  #Reading the HTML code from the website
  URL <-  tryCatch(read_html(URL), 
           error = function(e) force(do.next))

  df_list[[j]] <- getProfile(URL)
  j <- j + 1
}
final_df <- rbind.fill(df_list)
在tryCatch函数中,使用以下参数:

force(do.next)

这是基于另一个

我认为您需要围绕read_html调用编写一个tryCatch。这就是你的函数试图连接到网站并下载数据的地方。第二个错误不是错误,它只是关于你打开的连接的警告,不应该对结果产生任何影响。你说的对,期末考试看起来和预期的不一样。明天我会在tryCatch上尝试这个建议!谢谢你的评论。你为什么不回答他们?
  j <- 1
MHP_codes <-  c(150000:150200) #therapist identifier
df_list <- vector(mode = "list", length(MHP_codes))
for(code1 in MHP_codes) {
  delayedAssign("do.next", {next})
  URL <- paste0('https://www.psychologytoday.com/us/therapists/illinois/', code1)
  #Reading the HTML code from the website
  URL <-  tryCatch(read_html(URL), 
           error = function(e) force(do.next))

  df_list[[j]] <- getProfile(URL)
  j <- j + 1
}
final_df <- rbind.fill(df_list)
delayedAssign("do.next", {next})
force(do.next)