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

后端 未结 1 782
误落风尘
误落风尘 2020-12-11 04:14

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

相关标签:
1条回答
  • 2020-12-11 04:52

    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"
    
    0 讨论(0)
提交回复
热议问题