Leni Ohnesorge
Leni Ohnesorge

Reputation: 726

check if url is to HTML in R

What is the best way to check if url is to HTML site in R?

Currently I use:

x <- "http://google.com/
fileUrl <- (x)     
htmlTreeParse(fileUrl, useInternal=T)

This will return error, if URL is not HTML. However, I was wondering if there more proper way?

Upvotes: 2

Views: 173

Answers (1)

hrbrmstr
hrbrmstr

Reputation: 78832

You can try to use HEAD from the httr package to inspect the Content-Type the server says it will return:

library(httr)
resp <- HEAD("http://google.com/")
resp$headers$`content-type`
## [1] "text/html; charset=ISO-8859-1"

Upvotes: 4

Related Questions