如何确定url对象是否返回"404 Not Found"? [英] How to determine if a url object returns '404 Not Found'?

查看:111
本文介绍了如何确定url对象是否返回"404 Not Found"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单地说:如果

x <- read.csv(url)

存在,则R将返回该URL的内容.如果想尝试,一个很好的例子可能是"

exists, then R will return the contents of that url. A good example, if you want to try it, might be "http://ichart.finance.yahoo.com/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("http://google.com")
[1] TRUE
R> url.exists("http://csgillespie.org")
[1] FALSE

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

Alternatively, you could use the httr package

R> library(httr)
R> http_status(GET("http://google.com"))
$category
[1] "success"

$message
[1] "success: (200) OK"

R> http_status(GET("http://csgillespie.org"))
$category
[1] "server error"

$message
[1] "server error: (503) Service Unavailable"

这篇关于如何确定url对象是否返回"404 Not Found"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆