如何确定url对象是否返回"404 Not Found"?

编程入门 行业动态 更新时间:2024-10-24 20:22:02
本文介绍了如何确定url对象是否返回"404 Not Found"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

简单地说:如果

x <- read.csv(url)

存在,则R将返回该URL的内容.如果想尝试,一个很好的例子可能是" ichart.finance.yahoo/table.csv?s=IBM&a = 00& b = 1& c = 2008& d = 03& e = 4&f = 2014& g = d& ignore = .csv ".如果将该特定的URL分配给url并按上述方式运行,它将从Yahoo网站上将包含过去5年IBM股票数据的data.frame加载到x中.

exists, then R will return the contents of that url. A good example, if you want to try it, might be "ichart.finance.yahoo/table.csv?s=IBM&a=00&b=1&c=2008&d=03&e=4&f=2014&g=d&ignore=.csv" . That particular url, if assigned to url and run as above, will load up a data.frame into x from the Yahoo website containing the past 5 years of IBM stock data.

但是,如何事先告诉您是否有给定的网址会让您得到404错误?

But how to tell, beforehand, if any given url will get you 404'd ?

类似:

is.404.or.not(url)

或者也许

status(connect.to(url))

谢谢!

推荐答案

您可以使用RCurl软件包:

R> library(RCurl) Loading required package: bitops R> url.exists("google") [1] TRUE R> url.exists("csgillespie") [1] FALSE

或者,您可以使用httr软件包

Alternatively, you could use the httr package

R> library(httr) R> http_status(GET("google")) $category [1] "success" $message [1] "success: (200) OK" R> http_status(GET("csgillespie")) $category [1] "server error" $message [1] "server error: (503) Service Unavailable"

更多推荐

如何确定url对象是否返回"404 Not Found"?

本文发布于:2023-11-16 15:04:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605367.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   url   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!