Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

r - How to determine if a url object returns '404 Not Found'?

Simply put: if

x <- read.csv(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.

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

something like:

is.404.or.not(url)

or maybe

status(connect.to(url))

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use the RCurl package:

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

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...